How to create legend with multiple variables in a loop ?

21 visualizaciones (últimos 30 días)
Syam MS
Syam MS el 30 de Mzo. de 2021
Respondida: Rik el 30 de Mzo. de 2021
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

Respuestas (1)

Rik
Rik el 30 de Mzo. de 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.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by