MATLAB Answers

Using Hold with plotyy and subplot in loop

8 views (last 30 days)
Adrian
Adrian on 4 Feb 2014
I am trying to create a subplot that adds new lines to the charts with every pass of the loop.
There is one plotyy within the subplot that will not add two new lines, it will add one, but not a new line to each axis.
I have tried everything I could manage to find everywhere and nothing is working.
subplot(2,2,1)
h=plot(time, temp(suffix_start:suffix_start+l),time, tc1(suffix_start:suffix_start+l));
set(h,{'DisplayName'},{sprintf('Batch Temp %s\n',suffix1),sprintf('Thermocouple %s\n',suffix1);}')
set(gca,'NextPlot','replacechildren')
title('Temperature')
xlabel('Time (sec)')
ylabel('Temperature ^oC')
legend('toggle','location', 'SouthEast')
if strcmpi(over, 'yes')
hold on
end
subplot(2,2,2)
[AX,H1,H2]=plotyy(time, ram_position(suffix_start:suffix_start+l),time, power(suffix_start:suffix_start+l),'plot');
if strcmpi(over, 'yes')
hold(AX(1),'on')
hold(AX(2),'on')
end
title('Power')
set(get(AX(2),'Ylabel'),'String','Power (KW)')
set(get(AX(1),'Ylabel'),'String','Ram Position')
set(AX(1),'ylim',[0 105]);
xlabel('Time (sec)')
set(H1,'LineStyle','--')
legend('Ram', 'Power', 'location','NorthEast')
subplot(2,2,3)
plot(time, energy, 'DisplayName',sprintf('Suffix %s\n', suffix1),'color',RC)
title('Energy')
xlabel('Time (sec)')
ylabel('KW-hr')
legend('toggle')
if strcmpi(over, 'yes')
hold on
end
subplot(2,2,4)
plot(time, rpm(suffix_start:suffix_start+l), 'DisplayName',sprintf('Suffix %s\n',suffix1),'color',RC)
title('Mixer RPM')
xlabel('Time (sec)')
ylabel('RPM')
legend('toggle')
if strcmpi(over, 'yes')
hold on
end
p=mtit(name);
set(p.th,'fontweight','bold','fontsize',16);
This is the entire section of code, I know, I just pulled it from my section of code so it will not actually run because there are a lot of undefined variables in there. I can update it if it is going to be a huge problem.
The subplot in question is #2.
Thanks in advance for any advice!
  1 Comment
dpb
dpb on 4 Feb 2014
Nothing obvious there but there's no loop nor is there any attempt to add to the particular axes so there's nothing to see of any use to try to diagnose from.
Build a contained single plot example that demonstrates the issue and post it--if it's only the one, you don't need all the rest and subplot() itself probably has nothing to do with the issue.

Sign in to comment.

Answers (1)

José Crespo Barrios
José Crespo Barrios on 25 Apr 2014
Hello Adrián,
It is well known that an image is better than thousand words. In coding is the same, but examples instead of images :) Here is the solution. (Hint: use the indent carefully to keep the hierarchy between figure objets).
% Auxiliar lines for plotting
x = 0:0.01:2;
y11 = ( exp(-(x-1.2).^2) ).^8;
y12 = 1.1*y11;
y21 = x.^2;
y22 = 1.1*y21;
% figure 1.
h1 = figure(1);
whitebg(h1,'k')
% subplot 1.1.
h1_sp1 = subplot(2,1,1);
[ax,h1,h2] = plotyy(x,y11, x,y21);
set(h1, 'Color', [1 0 0], 'LineStyle', '-')
set(h2, 'Color', [1 0 0], 'LineStyle', 'o', 'MarkerSize', 1)
set(ax(1), 'ycolor', [1 1 1])
set(ax(2), 'ycolor', [1 1 1])
hold(ax(1))
plot(ax(1),x,y12,'g')
hold(ax(2))
plot(ax(2),x,y22,'og', 'MarkerSize', 1)
hold off
% end of subplot 1.1
% subplot 1.2.
h1_sp2 = subplot(2,1,2);
plot(x,y11)
hold on
plot(x,y12)
hold off
% end of subplot 1.2
% end of figure 1.
I hope you find this example useful.
Regards, José Crespo

Community Treasure Hunt

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

Start Hunting!