Legend is applied to incorrect figure

12 views (last 30 days)
agentroadkill
agentroadkill on 22 Nov 2015
Commented: Walter Roberson on 27 Nov 2015
So, I run several simulations in SimuLink, store lots of variables and them plot them variously on different figures. I would like some of the same plots to appear on different figures, so I've been doing something like:
%%plot results
% plot ideal sim
figure(1), hold on
h_step = plot(time, dtheta_ideal/(2*pi)*60, 'b') % dtheta in rpm
xlabel('Time (s)'); ylabel('Speed (RPM)');
title('Ideal second order model of DC motor')
legend ('open loop step response')
% plot idealPID0 sim
figure(2), hold on
hstep = plot(time, dtheta_ideal, 'b')
hKp = plot(time, dtheta, 'g');
hv_demand_Kp = plot(time, control, 'm')
hKp_ref = plot([0 time(end)], [ref ref], 'r')
plot(time, refsig, 'r')
xlabel('Time (s)'); ylabel('response');
title('Speed control of ideal 2nd order DC motor with Kp = 0.9 and Ki = 0')
legend([h_step, hKp_ref, hKp, hv_demand_Kp],...
{'Open loop step response (rad/s)',...
'reference signal (rad/s)',...
'proportional control response',...
'proportional control demand (V)'})
% repeat ad nauseam %
I would like and expect this to generate two plots, titled and with appropriate legends. What ACTUALLY happens is the legend designated for figure 2 shows up on figure 1, with no warnings written to the output (i.e. 'Open loop step response', 'reference signal' etc. show up on figure 1). The original legend for figure 1 is gone, and all the labels appear as I would 'expect' them to. Is there a way to explicitly specify which figure to put a plot on, or am I doing something deserving of an RTFM?

Answers (2)

Image Analyst
Image Analyst on 22 Nov 2015
Looks like it should work, but if it doesn't, you can explicitly pass in the axes to plot() and legend():
hFig2 = figure(2);
legend(hFig2, [h_step, hKp_ref, hKp, hv_demand_Kp],...
{'Open loop step response (rad/s)',...
'reference signal (rad/s)',...
'proportional control response',...
'proportional control demand (V)'})
  2 Comments
agentroadkill
agentroadkill on 27 Nov 2015
This yields an error: Handle inputs of type Figure cannot be used with this function
Walter Roberson
Walter Roberson on 27 Nov 2015
You cannot pass figures to legend(). legend() will examine the ancestor() 'figure' of the handles that you pass in to determine which figure to draw the legend in.

Sign in to comment.


Star Strider
Star Strider on 22 Nov 2015
I cannot run your code, but the reason the second legend call shows up in your first plot (with the ‘h_step’ handle) is that you told it to:
legend([h_step, hKp_ref, hKp, hv_demand_Kp],...
^^^^^^
  2 Comments
agentroadkill
agentroadkill on 27 Nov 2015
Ah. I fudged my own syntax. Thank you
Star Strider
Star Strider on 27 Nov 2015
My pleasure.
It’s the benefit of my seeing it for the first time.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!