How do I combine multiple plots that use hAxes = 'NextPlot' using subplot?

12 views (last 30 days)
Here is the code I have been using to create plots:
%% Plot1
s = [186,561,831,864,865,870,943,997,1001,1049,1055,1063,1077, 1078, 1171,1180,1190,1494,1497,1499,1522];
p = M(:,s)
figure;
hAxes = axes('NextPlot', 'add');
for ii = 1:size(s, 2)
j = s(1,ii)
plot(p(:, ii));
axis([0 600 0 35000])
set(gca,'xtick',[],'ytick',[])
str = {'SA = 21','Contour 260'};
annotation('textbox', [0.8, 0.1, 0.1, 0.1], 'String', str , 'linestyle', 'none')
end
%% Plot2
s = [181, 183, 192, 194, 199, 200, 201, 207, 213, 214, 220, 229, 230, 231, 235];
p = M(:,s)
figure;
hAxes = axes('NextPlot', 'add');
for ii = 1:size(s, 2)
j = s(1,ii)
plot(p(:, ii));
axis([0 600 0 35000])
set(gca,'xtick',[],'ytick',[])
str = {'SA = 15','Contour 64'};
annotation('textbox', [0.8, 0.1, 0.1, 0.1], 'String', str , 'linestyle', 'none')
end
How do I combine these two in one page (using subplot?)
  1 Comment
dpb
dpb on 14 Mar 2021
s = [186,561,831,864,865,870,943,997,1001,1049,1055,1063,1077, 1078, 1171,1180,1190,1494,1497,1499,1522];
p = M(:,s)
figure;
hAxes = axes('NextPlot', 'add');
for ii = 1:size(s, 2)
j = s(1,ii)
plot(p(:, ii));
axis([0 600 0 35000])
set(gca,'xtick',[],'ytick',[])
str = {'SA = 21','Contour 260'};
annotation('textbox', [0.8, 0.1, 0.1, 0.1], 'String', str , 'linestyle', 'none')
end
is the same as
s = [186,561,831,864,865,870,943,997,1001,1049,1055,1063,1077, 1078, 1171,1180,1190,1494,1497,1499,1522];
figure;
plot(M(:,s);
axis([0 600 0 35000])
set(gca,'xtick',[],'ytick',[])
str = {'SA = 21','Contour 260'};
annotation('textbox', [0.8, 0.1, 0.1, 0.1], 'String', str , 'linestyle', 'none')
Dont' need the temporaries nor a loop.

Sign in to comment.

Answers (1)

dpb
dpb on 14 Mar 2021
Edited: dpb on 14 Mar 2021
%% Plot1
s=[186,561,831,864,865,870,943,997,1001,1049,1055,1063,1077, 1078, 1171,1180,1190,1494,1497,1499,1522];
hAx(1)=subplot(2,1,1);
plot(M(:,s);
axis([0 600 0 35000])
set(gca,'xtick',[],'ytick',[])
str = {'SA = 21','Contour 260'};
annotation('textbox', [0.8, 0.1, 0.1, 0.1], 'String', str , 'linestyle', 'none')
%% Plot2
s = [181, 183, 192, 194, 199, 200, 201, 207, 213, 214, 220, 229, 230, 231, 235];
hA(2)=subplot(2,1,2);
plot(M(:,s);
axis([0 600 0 35000])
set(gca,'xtick',[],'ytick',[])
str = {'SA = 15','Contour 64'};
annotation('textbox', [0.8, 0.1, 0.1, 0.1], 'String', str , 'linestyle', 'none')
You could shorten further by putting the two s vectors and strings into cell arrays and selecting those in a loop over the two subplots--
s=[{[186,561,831,864,865,870,943,997,1001,1049,1055,1063,1077,1078, 1171,1180,1190,1494,1497,1499,1522]}; ...
{[181, 183, 192, 194, 199, 200, 201, 207, 213, 214, 220, 229, 230, 231, 235]}];
str = [{'SA = 21','Contour 260'};{'SA = 15','Contour 64'}];
for i=1:2
hAx(i)=subplot(2,1,i);
plot(M(:,s{i});
axis([0 600 0 35000])
set(gca,'xtick',[],'ytick',[])
hAnn(i)=annotation('textbox', [0.8, 0.1, 0.1, 0.1], 'String', str(i) , 'linestyle', 'none', ...
'horizontalalignment','right');
end
  2 Comments
dpb
dpb on 14 Mar 2021
If this did answer/solve the question/problem, then go ahead and "ACCEPT" the answer--if nothing else it shows others there is a solution.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!