Attempted to access colors(6); index out of bounds because numel(colors)=5
Ältere Kommentare anzeigen
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
Sara
am 24 Jun. 2014
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
am 24 Jun. 2014
Sara
am 24 Jun. 2014
What do you mean that the legend does not match the plot?
Sara
am 24 Jun. 2014
Attach x and y, with random data it works fine. Are x and y multidimensional arrays by any chance?
Sara
am 24 Jun. 2014
You'll need to include all the variables and the files, or I won't be able to run your code
Akzeptierte Antwort
Weitere Antworten (1)
Sara
am 24 Jun. 2014
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 Kommentar
Priya
am 25 Jun. 2014
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!