# How to create legend with multiple variables in a loop ?

14 ビュー (過去 30 日間)
Syam MS 2021 年 3 月 30 日

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

サインインしてコメントする。

### 回答 (1 件)

Rik 2021 年 3 月 30 日
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.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by