Attempted to access colors(6); index out of bounds because numel(colors)=5

Hi, I tried executing this loop,
colors = ['m';'y';'b';'g';'c'];
loop_ind = 1;
for k1 = 1:5
figure(4);
linespec = colors(loop_ind);
plot(x, y, linespec, 'LineWidth', 1);
loop_ind = loop_ind + 1;
hold on
end
legend('-0.02','-0.01','0','0.01','0.02')
But I get an error,
Attempted to access colors(6); index out of bounds because numel(colors)=5. Error in test_contact_ellipse (line 228)
linespec = colors(loop_ind);
Now I'm getting the plot with the correct colors but the legend doesn't appear. How can I rectify this error.
Please give your suggestions.
Thanks

5 Kommentare

I don't get any error, but I'd eliminate loop_ind and use linespec = colors(k1) and move figure(4) outside of the loop.
You may not get any error but you can see that the legend don't match with the plot.
I tried what you have suggested but it's not working still.
What do you mean that the legend does not match the plot?
Attach x and y, with random data it works fine. Are x and y multidimensional arrays by any chance?
You'll need to include all the variables and the files, or I won't be able to run your code

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

This is an example that plots in different colors with a matching legend:
c = [0 1 0; 1 0 0; 0 0 1; 1 0 1; 0 1 1];
x = linspace(0,2*pi);
figure(4)
hold on
for k1 = 1:size(c,1)
plot(x, sin(x*k1)+k1, 'Color', c(k1,:))
end
hold off
grid
legend('Color 1', 'Color 2', 'Color 3', 'Color 4', 'Color 5')
Adapt it as necessary to your application.

9 Kommentare

what do I need to replace
for k1 = 1:5
should it be
for k1 = 1:size(c,5)?
No. Keep the for statement in your code as it is. Just be sure that you have at least as many colors in your ‘c’ matrix as you have data sets to plot.
My initial use of randperm was because you wanted three non-repeating values from your xi array. If you want them all, simply choose them all. You don’t need randperm or anything else for that.
I assume from your comment to Sara’s answer, my idea for matching colors and functions didn’t work?
This does:
c = 'mrkgc';
x = linspace(0,2*pi);
figure(4)
hold on
for k1 = 1:size(c,2)
plot(x, sin(x*k1)+k1, 'Color', c(k1))
end
hold off
grid
legend('M', 'R', 'K', 'G', 'C')
Priya
Priya am 25 Jun. 2014
Bearbeitet: Priya am 25 Jun. 2014
Great! Thank you so much.
One more question, since two of the ellipses overlap, I want to give a symbol for one ellipse to highlight the overlapping. Please tell me how to include the symbol to get it displayed in legend.
Also, the symbol should hold the color which is hiding in the plot and again this should reflect in the legend.
My pleasure!
You would need to make some minor changes in your plotting code. The ‘c’ vector becomes a matrix with space for the line and marker designations, and the reference to it in the plot line changes:
c = [' m'; ' r'; ' k'; '-pg'; '-sc'];
x = linspace(0,2*pi);
figure(4)
hold on
for k1 = 1:size(c,1)
plot(x, sin(x*k1)+k1, c(k1,:))
end
hold off
grid
legend('M', 'R', 'K', 'G', 'C')
I believe this matches what you mentioned in your comment to Sara’s answer, but you may want to change the markers. I used a five-pointed star for the fourth (green) value, and a square for the fifth (cyan) value. They all plot lines as well as the markers.
This works:
colors = ['-pc'; ' r'; ' g'; '-sk'; ' m'];
Copy and paste it exactly to your code. There have to be two spaces where there is no marker-and-line designation.
This should work:
plot(x, y, colors(k2,:), 'LineWidth', 1);
Thanks again. It worked.
Again, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

In the last loop, replace with the following and see if it is what you are looking for:
figure(4);
plot(x, y, 'LineWidth', 1,'color',colors(k2))
axis square;
axis equal;
grid on;
xlabel('Longitudinal');ylabel('Lateral');title('Contact ellipse shape');
hold on
myh = line([xCenter, xCenter], [yCenter - yRadius(k2), yCenter + yRadius(k2)], ...
'LineWidth', 1, 'Color', [1,0,0]);
set(get(get(myh,'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
myh = line([xCenter - xRadius(k2), xCenter + xRadius(k2)], [yCenter, yCenter], ...
'LineWidth', 1, 'Color', [1,0,0]);
set(get(get(myh,'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
It seems that some of your ellipses are overwritten with others. Since I don't know what you are doing, this is the best I can recommend. I'd replace also:
for k2 = 1:size(a,2)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by