How to name multiple plots/subplots on a loop?
Mostrar comentarios más antiguos
This loop plots 1 plot with 3 subplots on the same Figure ii times. I cannot figure out how though to label each of the 4 plot with a unique name as the loop progresses. I can use the title, but it only names one of the plots which leaves the other 3 in question to which they are.
for iv = mod((1:numel(k)),12)
k_i = k(iv);
figure (k_i)
DataAnalPlot([c(1:ii,k_i) p(1:ii,k_i)],[c(1:ii,k_i+1) p(1:ii,k_i+1)],[c(1:ii,k_i+2) p(1:ii,k_i+2)],[c(1:ii,k_i+3) p(1:ii,k_i+3)]);
end
Respuesta aceptada
Más respuestas (1)
Nicolas Braconi
el 30 de Nov. de 2020
1 voto
For any future visitors I figured out that you can do it this way: Create a cell with the title you want each plot to take, for example months, Note that the number of entries in your cell should match the number of subplots, 1 title for each plot
cellmonth={'January','February','March','April','May','June','July'...
,'August','September','October','November','December'};
then you write a for loop with the number of subplots you want
for i=1:12
title(cellmonth(1,j))
.
.
.
.
end
3 comentarios
Image Analyst
el 1 de Dic. de 2020
You need to use i, not j in cellmonth(). Corrected code:
cellmonth = {'January','February','March','April','May','June','July', ...
'August','September','October','November','December'};
% Then you write a for loop with the number of subplots you want
for k = 1 : 12
subplot(4, 3, k);
title(cellmonth(k));
end

Yes, you can put in a cell, like you showed, or you can put in a string like I showed - it works either way. And you can predefine the strings in advance (like you did), or you can make up the string inside the loop if it depends on some variable that changes within the loop (like I did):
for k = 1 : 12
subplot(4, 3, k);
caption = sprintf('Month #%d', k); % Caption changes depending on k
title(caption, 'FontSize', 20);
end

Rt Ro
el 21 de Dic. de 2020
Hi, thanks for the answer. could you please let me know how I can move each title to up on the left?
Image Analyst
el 22 de Dic. de 2020
Try this:
for k = 1 : 12
subplot(4, 3, k);
caption = sprintf('Month #%d', k); % Caption changes depending on k
title(caption, 'FontSize', 20, 'HorizontalAlignment', 'right');
end

I have no idea why you say 'right' when you want to put it on the left. Seems like a bug.
Categorías
Más información sobre Subplots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!