problem with hold on into a subplot inside a for loop
3 views (last 30 days)
Show older comments
hi,I've a problem with the command 'hold on' into a subplot inside a for loop, it don't give me the overlap of the second curve of each graph. In the script I take many data from excel:
function cnr3
sheetnames = { '15x5_003','15x5_004','15x5_005' };
n = length(sheetnames);
%plotta p-theta e segnale inj
for idx = 1:n
A = xlsread('prove.xlsx',sheetnames{idx},'A3:A3603');
B = xlsread('prove.xlsx',sheetnames{idx},'B3:B3603');
C = xlsread('prove.xlsx',sheetnames{idx},'C3:C3603');
hold on
subplot(1,3,1)
[ax,h1,h2] = plotyy(A,B,A,C)
set(ax(1), 'XLim',[-60 60]);
set(ax(2), 'XLim',[-60 60]);
set(ax(1), 'YLim',[0 inf]);
set(ax(2), 'YLim',[0 120]);
set(ax(1), 'YTick',[0:20:240]);
set(ax(2), 'YTick',[0:20:120]);
xlabel('CA [deg]')
ylabel(ax(1),'p cyl [bar]')
ylabel(ax(2),'EC [A]')
title('1500x5')
hold off
end
for idx = 1:n
%plotta HR [%] e HRR[%/deg]
[A]=xlsread('prove.xlsx',sheetnames{idx},'A3:A3603');
[D]=xlsread('prove.xlsx',sheetnames{idx},'D3:D3603');
[E]=xlsread('prove.xlsx',sheetnames{idx},'E3:E3603');
hold on
subplot(1,3,2)
[ax,h1,h2] = plotyy(A,D,A,E)
set(ax(1), 'XLim',[-30 60]);
set(ax(2), 'XLim',[-30 60]);
set(ax(1), 'YLim',[-10 100]);
set(ax(2), 'YLim',[-inf inf]);
set(ax(1), 'YTick',[0:20:120]);
set(ax(2), 'YTick',[-10:4:50]);
xlabel('CA [deg]')
ylabel(ax(1),'HR [%]')
ylabel(ax(2),'HRR [%/deg]')
title('1500x5')
hold off
end
for idx = 1:n
%plotta HR [kJ/m^3]e HRR[kJ/m^3 deg]
[A]=xlsread('prove.xlsx',sheetnames{idx},'A3:A3603');
[F]=xlsread('prove.xlsx',sheetnames{idx},'F3:F3603');
[G]=xlsread('prove.xlsx',sheetnames{idx},'G3:G3603');
hold on
subplot(1,3,3)
[ax,h1,h2] = plotyy(A,F,A,G)
set(ax(1), 'XLim',[-30 60]);
set(ax(2), 'XLim',[-30 60]);
set(ax(1), 'YLim',[-inf inf]);
set(ax(2), 'YLim',[-inf inf]);
set(ax(1), 'YTick',[0:200:6000]);
set(ax(2), 'YTick',[0:15:600]);
xlabel('CA [deg]')
ylabel(ax(1),'HR [kJ/m^3]')
ylabel(ax(2),'HRR [kJ/m^3 deg]')
title('1500x5')
hold off
end
At the end,it give me this:

but it's incomplete,I would have somthing like this for all three subplots:

I will be very grateful for every your edvice
0 Comments
Answers (1)
dpb
on 21 Aug 2016
Edited: dpb
on 22 Aug 2016
Several issues here...
First, regarding hold and its use -- in general it's undesirable to set hold on until after the first plot into the given axes object is made via plot or other high-level plotting routine.
Secondly, it ONLY affects one axes at a time; for some reason I've never fathomed, TMW did not vectorize it; you can only set one axes at a time with hold...and plotyy creates two axes so to add to each of those, both axes' nextplot property must be set. This is either going to be
hold(ax(1)'on'), hold(ax(2)'on')
or,
set(ax,'nextplot','add')
for simultaneously setting both.
But, compounding the issue, you're overwriting the axes and line handles each pass through the loop so you've lost the handles to the first set of axes the next time through by not incrementing a counter to make them an array...
The better way would be to read the data into arrays, then plot it all in "one swell foop" instead...
for idx = 1:n
dat=xlsread('prove.xlsx',sheetnames{idx}); % get all the data from spreadsheet
A(:,idx)=dat(:,1); % create some local variables of
B(:,idx)=dat(:,2); % the desired columns from
C(:,idx)=dat(:,3); % multiple worksheets
end
subplot(1,3,1)
[hAx(1,:),hL1(1,:),hL2(1,:)]=plotyy(A,B,A,C);
linkaxes(ax,'x') % link the x axes to keep in synch
set(ax(1), 'XLim',[-60 60]);
set(ax(1), 'YLim',[0 inf],'YTick',[0:20:240])
set(ax(2), 'YLim',[0 120])'YTick',[0:20:120])
xlabel('CA [deg]')
ylabel(ax(1),'p cyl [bar]')
ylabel(ax(2),'EC [A]')
title('1500x5')
Now you don't actually need hold at all; you may want to set line colors manually, not sure how later releases treat, but probably still will cycle the multiple lines on the two axes. If so, then
set(hhL1(1,:),'color','b'),set(hL2(1,:),'color','g')
will do the trick.
Rearrange the other subplots similarly excepting, of course, use the correct columns out of the dat array as desired and keep the handles of the axes and lines in 2D arrays (or cell array if aren't same number of lines in each subplot).
0 Comments
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!