Using a variable where a string is required

Hi there. I'm starting to use MATLAB again after some time away from it and have a simple question I hope someone can help with. I have a large data set with many columns that I would like to plot, and then use the plot browser to examine particular elements of the data set. Is there a way to have my looped integer variable k appear as the correct number in the "DisplayName" field? My script is below.
Also, can anyone recommend a way for looping through colors? As the script is below all of the plots appear blue. I know that MATLAB will automatically cycle through colors if multiple Y's are given in the plot command, but is there a way to do both at the same time, loop through the colors and also write a unique "DisplayName"?
Thanks in advance for any help you can offer!
k=6;
hold on
plot(time,e129a_trpshift,'DisplayName','TRP Shift')
while (k<12)
plot(time,e129a_ResidueVsTime(:,k),'DisplayName','k')
k=k+1;
end
Best Wishes, Nathan

 채택된 답변

Matt Fig
Matt Fig 2011년 4월 6일

0 개 추천

If you want to use the built-in plot colors, do (for example):
A = {'k','b','r','g','y','c','k','b','r','g','y','c'};
x = 0:.01:1;
hold on
k = 1;
while k<=12,
plot(x,x.^k,'DisplayName',sprintf('%i',k),'color',A{k})% Example...
k = k+1;
end
legend show % Show DisplayNames
Otherwise you could define a 12-by-3 array of rgb values for the colors and index into that by row.

댓글 수: 1

Nathan Scott
Nathan Scott 2011년 4월 6일
Thanks so much Matt, this worked perfectly. sprintf() was exactly what I was looking for/trying to remember, and your solution for the coloring will work perfectly for me as well. Thanks again!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by