MATLAB: How to do a subplot loop

contourffor loopsubplot

Hi, I'm trying to subplot whithin a for loop. I'm asking matlab tu plot me three contourf called nzero_mean1, nzro_mean2, nzro_mean3 in a single subplot and each countourf has a title. But I don't know how to make those trhee contourf at one subplot in a forloop. I need help. I'm currently doing it like this:
figure
subplot(1,3,1)
contourf(nzro_frec1)
set(gca,'XTickLabel',vec)
set(gca,'YTickLabel',vec)
title('Especie 1')
colorbar('southoutside')
subplot(1,3,2)
contourf(nzro_frec2)
set(gca,'XTickLabel',vec)
set(gca,'YTickLabel',vec)
title('Especie 2')
colorbar('southoutside')
subplot (1,3,3)
contourf(nzro_frec3)
set(gca,'XTickLabel',vec)
set(gca,'YTickLabel',vec)
title('Especie 3')
colorbar('southoutside')
[ax1,h1]=suplabel('Delta m3');
[ax2,h2]=suplabel('Delta m2','y');
[ax3,h3]=suplabel('Frecuencia de sobrevivencia. Alpha = 0. p=10. Respuesta Sigmoide (s=2)' ,'t');
set(h3,'FontSize',20)
set(h1,'FontSize',14)
set(h2,'FontSize',14)
orient portrait
print('-dps','suplabel_test')
%unix('convert suplabel_test.ps suplabel_test.jpg');

Best Answer

It wouldn't be worth it. You'd have to have if blocks in the loop to handle cases that should be different on every loop but don't depend on the loop iterator. Sure you could do
for k = 1 : 3
subplot(1, 3, k);
% code
caption = sprintf('Especie %d', k);
title(caption);
end
but the contourf, etc. are not a function of k so you'd have to say
if k == 1
contourf(nzro_frec1)
colorbar('southoutside')
elseif k == 2
contourf(nzro_frec2)
colorbar('southoutside')
etc. that essentially the looping is not gaining you anything.