Legend function is not properly indexing and displaying values.

7 views (last 30 days)
In my script, I let the user change two variables out of four variables i.e. gravitational constant(planet), angles, velocities.(The two other values are hardcoded in,velocity=100m/s).Those two variables can then be subject to how many parameters the user wants to test i.e two gravitational constants and four different angles.
For example lets say i picked two different gravity constants and two different angles.
I have all the correct calculations for the graphs and the graphs print out fine but however my legend command is not working like i was hoping. I need the legend command to print out on the graph x number of how many angles the user decided to test i.e. I need two angles to print out on the legend for each gravity constant tested.
Firstinv==4 is the option to choose gravity constant and Secinv==2 is the option to choose angles
Numplanet is how many planets or gravity constants were chosen and Numangle is how many angles were chosen.
if Firstinv==4 && Secinv==2
for p=1:Numplanet
for d=1:Diffstart
for v=1:Mvelocity
for k=1:Numangle
subplot(Numplanet,1,p)
plot(squeeze(x(p,k,1:tend(p,k))),squeeze(y(p,k,1:tend(p,k))))
hold on
axis([0 max(max(max(x))) 0 max(max(max(y)))])
legendtext{p,k}='';
legendtext{p,k}=[legendtext{p,k};strcat(num2str(thd(k)),' degrees, ',num2str(v0(v)),'m/s','x0= ',num2str(x0(d)),'y0= ',num2str(y0(d)))];
xlabel('Distance (meters)')
ylabel('Height (meters)')
legend(legendtext{p,:})
spr=sprintf('Distance vs. Height on %s (%0.3f m/s^2)',Nameplan{p},Planet(p));
title(spr)
end
end
end
end
end
I apologize if this question is terribly worded.
EDIT: Heres the error code that im getting and the data that is being inputed.
Enter the time increment in seconds= .1
How many planets would you like to investigate projectile motion on? 2
How many different angles? 2
Enter the angle in degrees= 45
Enter the angle in degrees= 60
Are your inputs correct? yes/no [yes] yes
Error using legend (line 120)
Invalid argument. Type 'help legend' for more information.
I need to have labels on the second graph giving me the Earth's projectile motion at 45 degrees and 60 degrees.
Thanks.
  1 Comment
Geoff Hayes
Geoff Hayes on 29 Jan 2016
braydon - please clarify what you mean by legend function is not properly indexing and displaying values. Are the wrong labels being applied (duplicated) or are incorrect colours being displayed (again duplicated)? Please provide more detail and perhaps a snapshot of what is appearing and describe what you would like to see. Looks at http://www.mathworks.com/matlabcentral/answers/247736-legend-has-different-color-lines-from-plot-lines which may provide a hint of what is going wrong.
While it is good to attach your code that generates the problem, without having the same data it will be difficult to pinpoint what the issue may be.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jan 2016
Replace
legend(legendtext{p,:})
with
legend(legendtext(p,1:k))
Your legendtext is a 2D cell array of strings, and it is being dynamically grown. It looks like it might work for the first planet, legendtext{1,:} but when you get to the second planet p{2,:} the second dimension has been grown but you have not defined values for legendtext{2,2:end} so those will be empty arrays [] rather than empty strings '' at that time you do legendtext{2,:}
Using legendtext{p,:} is also risky in that if you ever happened to have an entry that looked exactly like one of the option names for legend(), it would get interpreted as that option. It is therefore recommended that you put your legend strings in a cell array of strings and pass in that cell array as the first (non-handle) option rather than using {} expansion to pass them in as multiple arguments.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!