How to create legend with multiple variables in a loop ?
Ältere Kommentare anzeigen
I want to have a comparison between two plots when a parameter 'M' have different values. I need help in writing the legend properly.

The MATLAB code that i have written is as follows.
%% Input
pro=[1 2 3; 1 2 3;]; % Each row for one value of parameter
rate=[5 10 15;15 20 25;]; % Each row for one value of parameter
bs=pro;
bs_rate=[3 8 13;13 18 23;]; % Each row for one value of parameter
%% Plot
for sz = 1:1:2
figure(1)
p=plot(pro(sz,:),rate(sz,:),'-p',...
bs(sz,:),bs_rate(sz,:),'-->');
xlabel('x-axis')
ylabel('y-axis')
p(1).LineWidth = 2; p(2).LineWidth = 2;
hold on
grid on
Legend{sz}=strcat('Proposed, M =',num2str(sz),' Benchmark M =', num2str(sz));
legend(Legend);
end
Antworten (1)
Rik
am 30 Mär. 2021
0 Stimmen
You should put the figure creation and the creation of the legend outside the loop.
You are merging the two texts, but you should make each a cell element.
Kategorien
Mehr zu Legend finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!