Automatic generation of Legend

I have a program which asks users for vectors. These vectors are used to plot graphs based on previous data in the code. The user is able to specific as many numbers as possible and these are plotted. I want to add a legend that can automatically adjust to fit the number of points the user specifies. 'n' is the user specified vector
for n = [1 50 23 9]
ii = 11 +1;
plot(x, u(:,n+1))
xlabel('x(m)')
ylabel('t(s)')
title('Graph of U against x for specified time intervals')
legend (['t = ', num2str(n)])
grid on
hold on
end

 Accepted Answer

Star Strider
Star Strider on 10 Jun 2022
In the legend documentation, see the section on Specify Legend Labels During Plotting Commands.
Specifically here:
plot(x, u(:,n+1), 'DisplayName',sprintf('t = %2d',n))
and remove the legend call within the loop.
Then after the loop, the legend call is simply either:
legend
or:
legend('Location','best')
or something similar.
So the code would be something like this:
figure
hold on
for n = [1 50 23 9]
ii = 11 +1;
plot(x, u(:,n+1), 'DisplayName',sprintf('t = %2d',n))
xlabel('x(m)')
ylabel('t(s)')
title('Graph of U against x for specified time intervals')
grid on
end
hold off
legend('Location','best')
There are likely better ways to code the loop, however since this appears to be a section of larger code, I will leave it as is.
.

6 Comments

Thank you very much
Now aside the points of interest on the legend, it adds data 1, data 2, data 3 and data 4 also on the legend. So now there are eight entries instead of four in the legend.
As I am still learning, I dont mind finding robust ways ways of imrpvoing my code, like the loop you mentioned. I am open to ideas
My pleasure.
I still only have a portion of the actual code, so I have to guess as to what the problem is, and guess at a solution.
Try this —
figure
hold on
for n = [1 50 23 9]
ii = 11 +1;
hp{n} = plot(x, u(:,n+1), 'DisplayName',sprintf('t = %2d',n));
xlabel('x(m)')
ylabel('t(s)')
title('Graph of U against x for specified time intervals')
grid on
end
hold off
legend([hp{:}],'Location','best')
That should only display the legend entries for selected plot calls.
To illustrate with an example —
x = linspace(0, 150, 151);
v = (1:2:10).';
y = sin(v*2*pi*x/150);
figure
hold on
for k = 1:numel(v)
hp{k} = plot(x,y(k,:), 'DisplayName',sprintf('Frequency = %2d',v(k)));
end
legend([hp{1};hp{3};hp{5}], 'Location','best')
.
Thank you very much for your quick responses and your time. It is working fine now. What text do you recommend that I could learn to make my codes more robust?
As always, my pleasure!
I do not see the entire code, and I do not understand the reason ‘ii’ is created since it is never used. If you need to use a specific counter such as ‘ii’, the best way is likely something like this:
figure
nv = [1 50 23 9];
hold on
for ii = 1:numel(nv)
n = nv(ii);
hp{ii} = plot(x, u(:,n+1), 'DisplayName',sprintf('t = %2d',n));
xlabel('x(m)')
ylabel('t(s)')
title('Graph of U against x for specified time intervals')
grid on
end
hold off
legend([hp{:}],'Location','best')
With respect to learning how to write efficient code, take some time here on Answers and read the solutions to the posted problems. This is the best source I know of, and I have learned much here over the years.
.
Thanks a lot for the solution, advice, time and patience. I appreciate it
As always, my pleasure!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!