How to add serialized legends to a chart in MATLAB
Show older comments
I have a database with n wind speeds, then I classify those speeds in a variable called Nclassesdevento (in the example it was classified in 10 classes).
I am making a graph of the nclasses and I need the caption for those classes. Includes the Legend function in the for loop because I expected the legend to return for each class after each interaction but it didn't work.
In other words, it is necessary to include the legend for each respective class
Can someone help me?
figure(3)
subplot(2,1,1);
hold on
xaxa=0;
for kkkk=1:Nclassesdevento
plot(ventoclass(kkkk,1:nptv(kkkk)))
xaxa=xaxa+1
legenda(xaxa)=legend(['Classe ', num2str(kkkk)])
end
hold off
xlabel('Ocorrência dos ventos');
ylabel('Velocidade do Vento média (m/s)');
title(['Velocidade média do Vento SCADA categorizada em ', num2str(Nclassesdevento()),' classes'])
subplot(2,1,2);
histfit(ventoutil,50,'wbl')
xlabel('Velocidade media do vento (m/s)');
ylabel('numero de Ocorrência');
title('Histograma da velocidade media do vento e curva de Weibull');
legend({'Vento','Weibull'});

Answers (2)
Star Strider
on 8 Jun 2020
Edited: Star Strider
on 8 Jun 2020
0 votes
See if the Line Properties name-value pair DisplayName will do what you want. Specifically see: Specify Legend Labels During Plotting Commands for an illustration.
1 Comment
Star Strider
on 17 Jun 2020
My pleasure.
I cannot run your code without the appropriate data.
Try this:
plot (ventoclass (kkkk, 1: nptv (kkkk)), 'DisplayName',sprintf('Classe %2d',kkkk))
That worked when I tried it with this test code:
figure
hold on
for kkkk = 1:3;
plot(rand(1,2), rand (1,2), 'DisplayName',sprintf('Classe %2d',kkkk))
end
hold off
legend
.
Vinicius Teixeira
on 17 Jun 2020
Categories
Find more on Printing and Saving in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!