How to create legend with multiple variables in a loop ?
6 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Rik
on 30 Mar 2021
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.
0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!