Using DisplayName Within a Loop
20 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
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.
Asad (Mehrzad) Khoddam
2020-10-9
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!