plotting with changing variable values with switch case command

9 次查看(过去 30 天)
My whole program run with a loop . It is not showing the plot when I try below:
for nt =1:30001
...
switch nt
case 0
h1=plot(x,y,x,z,'-.xr');
case 10001
h2=plot(x,y,x,z,'-.xg');
case 20001
h3=plot(x,y,x,z,'-.xk');
case 22556
h4=plot(x,y,x,z,'-.xb');
end
hold on;
legend('1','2','3','4')
How can I plot all cases?

回答(1 个)

Robert Cumming
Robert Cumming 2015-3-3
you need to specify the hold command before you start creating the plots.
  2 个评论
Meva
Meva 2015-3-3
编辑:Meva 2015-3-3
I have added
plot(h1,h2,h3,h4)
I used :
switch nt
case 0
h1=plot(x,y,x,z,'-.xr');
case 10001
h2=plot(x,y,x,z,'-.xg');
case 20001
h3=plot(x,y,x,z,'-.xk');
case 22556
h4=plot(x,y,x,z,'-.xb');
end
hold on;
plot(h1,h2,h3,h4)
legend('1','2','3','4')
but it does not recognise h1
Robert Cumming
Robert Cumming 2015-3-3
your loop starts at 1 - h1 is only activated when your loop index is 0 (doesn't look like that happens in your example)

请先登录,再进行评论。

类别

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