Using DisplayName Within a Loop

20 views (last 30 days)
Rafael Fehér
Rafael Fehér on 9 Oct 2020
Commented: Rafael Fehér on 12 Oct 2020
I'm trying to plot multiple different legends to my plot. I'm trying the following code:
col_sim = {'-ro', '-bs', '-*k','-gx'}; % symbols for each curve of the plot (simulated data)
col_exp = {'ro', 'bs', '*k','gx'}; % symbols for each curve of the plot (experimental data)
gap_ratio = [2 3 4 5]; % vector for gap ratios tested
for k=1:n_beta
txt_sim = ['e/D = ',num2str(gap_ratio(1,k)), '(Simulation)'];
txt_exp = ['e/D = ',num2str(gap_ratio(1,k)), '(de Oliveira Barbosa et al., 2017)'];
plot(Red_V,DataRed_Amplitude_y(:,k),col_sim{k},data_gap2_barbosa_Ay_2D(:,k+i),data_gap2_barbosa_Ay_2D(:,k+i+1),col_exp{k},'DisplayName',txt_sim,'DisplayName',txt_exp)
hold all
grid on
legend show
i = i+1;
end
And Matlab is plotting the following image:
But all the legends are "(de Oliveira Barbosa et al., 2017)" and what I really want is "(Simulation)" in the first row and "(de Oliveira Barbosa et al., 2017)" in the second row, then again "(Simulation)" in the tirth row and "(de Oliveira Barbosa et al., 2017)" in the forth row and so on.
Can somenone helpe me with this code?
Thank you,
Rafael F.

Answers (2)

Walter Roberson
Walter Roberson on 9 Oct 2020
You cannot do that in a single call. For plot() the last name/value pair for any given option is the one used for all of the lines created. The linestyle/marker/color arguments that are not name/value pairs are the only ones that are associated with the most recent data only.
  3 Comments
Rafael Fehér
Rafael Fehér on 12 Oct 2020
Walter Roberson. Thank you for the explanation. I suspected it, and now you have confirmed it to me.

Sign in to comment.


Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam on 9 Oct 2020
col_sim = {'-ro', '-bs', '-*k','-gx'}; % symbols for each curve of the plot (simulated data)
col_exp = {'ro', 'bs', '*k','gx'}; % symbols for each curve of the plot (experimental data)
gap_ratio = [2 3 4 5]; % vector for gap ratios tested
for k=1:n_beta
txt_sim = ['e/D = ',num2str(gap_ratio(1,k)), '(Simulation)'];
txt_exp = ['e/D = ',num2str(gap_ratio(1,k)), '(de Oliveira Barbosa et al., 2017)'];
if mod(k,3) == 0
plot(Red_V,DataRed_Amplitude_y(:,k),col_sim{k},data_gap2_barbosa_Ay_2D(:,k+i),data_gap2_barbosa_Ay_2D(:,k+i+1),col_exp{k},'DisplayName',txt_sim)
else
plot(Red_V,DataRed_Amplitude_y(:,k),col_sim{k},data_gap2_barbosa_Ay_2D(:,k+i),data_gap2_barbosa_Ay_2D(:,k+i+1),col_exp{k},'DisplayName',txt_exp)
end
hold all
grid on
legend show
i = i+1;
end

Community Treasure Hunt

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

Start Hunting!