shouldPlot and switch case and legend and multiple figure error

2 次查看(过去 30 天)
Two questions about shouldPlot and switchcase:
1) I cannot locate correctly legends I mean it does not show legends for figure (1) 2) Figure (2) cannot be shown. I can usse another swicthcase but I do not want to overwrite.
shouldPlot = true;
switch nt
case 1
f = up; g = down;
c = middle;
vel1 = u1; vel2 = u2;
hold on;
case 2
f = up; g = down;
c = middle;
vel1 = u1; vel2 = u2;
otherwise
shouldPlot = false;
% disp('Quit');
end
if (shouldPlot)
figure (1);
plot(xx,f,xx,g);
legend('time 0','time 0','t clash','t clash')
hold on;
plot(xx, c);
% legend('time 0','t clash')
hold on
% axis([0 1 0 1]);
xlabel('x');
ylabel('body');
figure (2);
plot(xx,vel1,xx,vel2);
...
end

回答(1 个)

Michael Haderlein
When I run the code inside your if case with random values, I do get the legend in figure 1. Of course, only f and g are described, but that's all which is plotted at the time the legend is created. Depending on the size of f and g, the legend either includes only "time 0" twice or also "t clash". c is not included in the legend as you plot c after the creation of the legend. Remember that the legend will be fully rewritten when you use the legend command. Old entries will disappear if you do not repeat them.
What do you mean with "Figure (2) cannot be shown."? Figure 2 is created and shows up just as it should.
  4 个评论
Meva
Meva 2015-3-5
legend certainly does not work.. I have used :
shouldPlot = true;
switch nt
case 1
...
case 2
...
otherwise
shouldPlot = false;
...
end
if (shouldPlot)
figure (1);
plot(xx,f,xx,g);
hold on;
plot(xx, c);
legend('for f case1','for g case1','or f case2',
'for g case2','for c case 1','for c case2')
hold on
xlabel('...');
ylabel('...');
figure (2);
plot(xx,vel1,xx,vel2);
...
end
How can I show the lines names without legend is there another way?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by