Using DisplayName Within a Loop

17 次查看(过去 30 天)
Rafael Fehér
Rafael Fehér 2020-10-9
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.

回答(2 个)

Walter Roberson
Walter Roberson 2020-10-9
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 个评论
Rafael Fehér
Rafael Fehér 2020-10-12
Walter Roberson. Thank you for the explanation. I suspected it, and now you have confirmed it to me.

请先登录,再进行评论。


Asad (Mehrzad) Khoddam
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

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by