Add variable legend to a plot without for loop

I am plotting two 201x4 arrays, resulting in four 2-d lines. The y array is a time series so I want the legend to vary accordingly. I created a 1x4 cell with strings that show the time variation of my y variable. However, the result is not satisfactory as shown in the figure below:
My code attempt is below:
lgd = {'HGS 50-year','HGS 100-year','HGS 150-year','HGS 200-year'};
h2 = plot(x,y,'LineWidth',1.25,'DisplayName',char(lgd)');
legend;
I can easily accomplish what I need with a for loop but I do not want to do that because I think I am close to accomplishing what I need without it.
Please help. Thank you.

 Risposta accettata

Star Strider
Star Strider il 11 Mar 2021
Modificato: Star Strider il 11 Mar 2021
This appears to work correctly:
x = 0:0.1:10;
y = exp(-(x-(0:4).').^2)./(1:5).';
figure
plot(x, y)
lgd = {'HGS 50-year','HGS 100-year','HGS 150-year','HGS 200-year'};
legend(lgd)
EDIT — (11 Mar 2021 at 21:34)
Added plot figure.
.

2 Commenti

Kien Pham
Kien Pham il 11 Mar 2021
Modificato: Kien Pham il 11 Mar 2021
Thank you. I have a follow-up question after getting that to work. Can you take a look at my comment to Walter Robertson above and see if you have help me a bit further?
Yes.
Try this:
x = 0:0.1:10;
y = exp(-(x-(0:3).').^2)./(1:4).';
ya = exp(-(x-(0:3).'+0.1).^2)./(1:4).';
figure
hv = plot(x, y);
hold on
ha = plot(x, ya, '--k');
hold off
lgd = {'HGS 50-year','HGS 100-year','HGS 150-year','HGS 200-year'};
legend([ha(1); hv],{'Analytical',lgd{:}})
producing:
.

Accedi per commentare.

Più risposte (1)

N = 20;
x = 1:N;
y = sort(rand(N,4));
lgd = {'HGS 50-year','HGS 100-year','HGS 150-year','HGS 200-year'};
h2 = plot(x,y,'LineWidth',1.25);
legend(h2, lgd)

3 Commenti

Thank you @Walter Roberson. That helps. I have a follow-up question for you.
Suppose I have an h1 series plotted in which the time variation is not important.
I used the 'DisplayName' switch to name that series while plotting h1. Is there a way to have the legend shown for only one of the 4 series in h1 but show the time sensitive legend for all 4 of the series in h2?
This is what I want to show:
I attempted to do so in the legend command below but it gave me an error. Thanks again for your help.
N = 20;
x = 1:N;
y = sort(rand(N,4));
x2 = 1:N;
y2 = sort(rand(N,4));
lgd2 = {'HGS 50-year','HGS 100-year','HGS 150-year','HGS 200-year'};
h1 = plot(x2,y2,'k--','LineWidth',1.25,'DisplayName','Analytical'); hold on;
h2 = plot(x,y,'LineWidth',1.25);
hold off;
legend(h1(1); h2, lgd); %error here
N = 20;
x = 1:N;
y = sort(rand(N,4));
x2 = 1:N;
y2 = sort(rand(N,4));
lgd1 = {'Analytical'};
lgd2 = {'HGS 50-year','HGS 100-year','HGS 150-year','HGS 200-year'};
h1 = plot(x2,y2,'k--','LineWidth',1.25); hold on;
h2 = plot(x,y,'LineWidth',1.25);
hold off;
legend([h1(1); h2], [lgd1, lgd2]);
Very elegant and without the the use of {} at all as in the solution by Star Strider. Many thanks.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by