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

 采纳的回答

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 个评论

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 中查找有关 Graphics Object Properties 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by