Using DisplayName Within a Loop
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Walter Roberson
le 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 commentaires
Asad (Mehrzad) Khoddam
le 9 Oct 2020
also you can use legend function in every loop as:
Asad (Mehrzad) Khoddam
le 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
0 commentaires
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!