Get a plot to automatically load different colors using a matrix

10 views (last 30 days)
I am trying to plot 16 different sets of data onto 1 plot, and my adviser suggested concatenating the data I am using (amplitude data) and then when plotting it should load all 16 in different colors automatically. I was able to concatenate the data, but for some reason I can't get it to plot correctly. Here is a snippet of my data below:
load(FILENAME);
respidx = find(n1p2amp > THR_CRIT);
allamp = cat(2, n1p2amp,allamp);
plot (masker*ELEC_SPACE,allamp)%plots amplitude data
xlabel('Masker Electrode (re: electrode spacing)')
ylabel ('Amplitude (mV)');
text(masker(respidx(maxidx))*ELEC_SPACE,n1p2amp(respidx(maxidx)),[num2str(maskch)],...
'VerticalAlignment','middle',...
'HorizontalAlignment','right',...
'FontSize',10)
hold on;
set(gca,'XLim',[0*ELEC_SPACE 16*ELEC_SPACE],'YLim',[-0.1 0.6]);
set(gca, 'XTick',0:4*ELEC_SPACE:16*ELEC_SPACE);
This is all contained within the loop "i" which is essentially numbered 1:16. I have a 16x16 matrix for each subject, so the x-axis has 16 points with each point containing 16 data points, if that makes sense. I just don't want to have to manually tell this to do 16 different colors. Thanks!

Accepted Answer

Mike Garrity
Mike Garrity on 2 Oct 2014
I think your advisor is just referring to the fact that if you give plot a YData array which is an MxN array, then it will plot them as M curves and give each one its own color.
Consider this example:
d = gallery('moler',16,.5); % an interesting 16x16 matrix
Executing this:
plot(1:16,d);
creates the same thing as:
hold on
for i=1:16
plot(1:16,d(i,:));
end
and it also gives each of the curves a different color.
BTW, you can also get the second example to generate different colors by using "hold all" instead of "hold on".
Also, the plot command pulls its colors from the ColorOrder property of the axes rather than the colormap. The colormap is only used by objects which have CData, such as surface or image.
  1 Comment
Lindsay
Lindsay on 2 Oct 2014
Mike, thank you so much. I will try this later today and let you know how it went. I'm still relatively new to Matlab so your instructional response helps a great deal!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 2 Oct 2014
The easiest way is to specify the color when you plot. If you don't you get the default colors. Maybe you'd be interested in my ColorOrder demo where you can alter the default colors.
  3 Comments
Image Analyst
Image Analyst on 2 Oct 2014
You're welcome. Maybe you could "Vote" for the answer even if it didn't earn your "Acceptance", if it at least you learned something from it.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!