Using plot command in a loop

조회 수: 1 (최근 30일)
Yasmin Tamimi
Yasmin Tamimi 2012년 10월 19일
Hey All,
I tried to change the input argument for plot command but it didn't work as I discovered that plot doesn't accept it as an input argument:
for i= 1:20
figure, plot((sprintf('Data%d',i))) , hold on; plot(f_mean*ones(length(sprintf('Data%d',i)),1),'r'); plot(sprintf('alpha_data%d',i),'g'); plot(sprintf('alpha_data_neg%d',i),'g'); hold off; axis ([0 350 0.28 0.32])
end
What can I change in my code to make it work??
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 19일
you are trrying to plot a string. What data do you want to plot?

댓글을 달려면 로그인하십시오.

채택된 답변

Pedro Villena
Pedro Villena 2012년 10월 19일
편집: Pedro Villena 2012년 11월 8일
if you want to change the variable name in the loop, use eval command
for i= 1:20,
figure(i),
eval(sprintf('plot(Data%d)',i));
hold on;
eval(sprintf('plot(f_mean*ones(1,length(Data%d)),''r'')',i));
eval(sprintf('plot(alpha_data%d,''g'')',i));
eval(sprintf('plot(alpha_data_neg%d,''g'')',i));
hold off;
axis ([0 350 0.28 0.32])
end
  댓글 수: 1
Yasmin Tamimi
Yasmin Tamimi 2012년 10월 19일
Thanks a lot. Really appreciated!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

MANJUNATH
MANJUNATH 2012년 10월 19일
try using string to num conversion

Robert Cumming
Robert Cumming 2012년 10월 20일
using eval is almost always a bad idea. Store your data as
Data(1), Data(2) etc..... rather than Data1, Data2 etc....
If you end up needing to use eval to solve your problem your looking in the wrong area for a solution.
Why should you not use it:
1. Its hard to debug 2. Its very slow.
Have a look at Matlabs Loren telling you why to avoid eval here and here

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by