how to do a subplot loop?

1 view (last 30 days)
Beatriz Sanchez
Beatriz Sanchez on 5 Sep 2018
Commented: Beatriz Sanchez on 6 Sep 2018
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');

Accepted Answer

Image Analyst
Image Analyst on 5 Sep 2018
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.
  1 Comment
Beatriz Sanchez
Beatriz Sanchez on 6 Sep 2018
Thank yo very much, well I end up doing it like this:
for i=1:3
figure(1)
subplot(1,3,i)
if i==1
contourf(nzro_mean1)
elseif i==2
contourf(nzro_mean2)
elseif i==3
contourf(nzro_mean3)
end
axis square
set(gca,'XTickLabel',vec)
set(gca,'YTickLabel',vec)
title(['Especie ',num2str(i)])
colorbar('southoutside')
end
I think it's better that what I had.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!