shouldPlot and switch case and legend and multiple figure error

8 views (last 30 days)
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

Answers (1)

Michael Haderlein
Michael Haderlein on 5 Mar 2015
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 Comments
Meva
Meva on 5 Mar 2015
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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!