How to set line style while using plot in a for loop?

조회 수: 48 (최근 30일)
braulio
braulio 2011년 11월 13일
I am trying to plot some data from a structure usign a for loop. I want to set the "linetype" property every iteration. But I don't know how to pass the value of the property to the plot command.
Here is an example of the code:
for j=1:2
subplot(2,1,j)
for i=1:3
plot(sD{i}.t,sD{i}.V(:,j),'linestyle',linS{i}); hold on;
end
end
however, if I create the cell array
lin={'-','--',':'}
plot does not understand it is the value of line style property. I don't understand of it is possible to send a string to the property value. Is there a way?
The other option I explored is using "findall" to get line objects and change their properties, but still I guess I would not be able to do it in a loop.
I cannot "vectorize" the plot because the variables in the structure have different sizes.
Thanks for your help!

채택된 답변

Daniel Shub
Daniel Shub 2011년 11월 13일
With a slight change (changing lin to be linS) it works for me...
linS = {'-','--',':'};
for j=1:2
subplot(2,1,j)
for i=1:3
plot(randn(10, 1),'linestyle',linS{i}); hold on;
end
end
I also replaced what you are plotting with randn. What do you get?
  댓글 수: 1
braulio
braulio 2011년 11월 13일
Hej again! Thanks a lot once more ... the question had a typo, so I was using the right name of the variable "linS".
However, I was writing the a wrong property value because I was defining
linS={'-','--r','-k'}; %!!!!!
... so I should jut set color with color property :x
see you,
b

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by