Using a variable where a string is required

2 views (last 30 days)
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

Accepted Answer

Matt Fig
Matt Fig on 6 Apr 2011
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 Comment
Nathan Scott
Nathan Scott on 6 Apr 2011
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!

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!