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 件のコメント

Sara
Sara 2014 年 6 月 24 日
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.
Priya
Priya 2014 年 6 月 24 日
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.
Sara
Sara 2014 年 6 月 24 日
What do you mean that the legend does not match the plot?
Sara
Sara 2014 年 6 月 24 日
Attach x and y, with random data it works fine. Are x and y multidimensional arrays by any chance?
Sara
Sara 2014 年 6 月 24 日
You'll need to include all the variables and the files, or I won't be able to run your code

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2014 年 6 月 24 日
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 件のコメント

Priya
Priya 2014 年 6 月 24 日
what do I need to replace
for k1 = 1:5
should it be
for k1 = 1:size(c,5)?
Star Strider
Star Strider 2014 年 6 月 24 日
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.
Star Strider
Star Strider 2014 年 6 月 25 日
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 2014 年 6 月 25 日
編集済み: Priya 2014 年 6 月 25 日
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.
Star Strider
Star Strider 2014 年 6 月 25 日
編集済み: Star Strider 2014 年 6 月 25 日
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.
Star Strider
Star Strider 2014 年 6 月 25 日
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.
Star Strider
Star Strider 2014 年 6 月 26 日
This should work:
plot(x, y, colors(k2,:), 'LineWidth', 1);
Priya
Priya 2014 年 6 月 26 日
Thanks again. It worked.
Star Strider
Star Strider 2014 年 6 月 26 日
Again, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Sara
Sara 2014 年 6 月 24 日
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)

1 件のコメント

Priya
Priya 2014 年 6 月 25 日
Sara, thanks for your help, it's working now.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

質問済み:

2014 年 6 月 24 日

コメント済み:

2014 年 6 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by