How can I change the title of the subplots with forloop iteration?

I have 24 subplots as shown below. I want to give the title name (CB1000(m)) to the first 6 subplots in a row and then the next title name (CB2000(m)) to the next 6 subplots and so on. What actually I want is to change these title names according to the loop iteration "h=1000:1000:4000" but having the same title for the 6 subplots in a row. I'm using the syntax
title(sprintf('CB %d(m)',h))
but it's not suitable to use. How can I solve this problem? A part of my program is given below
%% draw all of the tables
figure
count = 1;
for h = 1000:1000:4000
for fov = [0.2, 0.5, 1, 2, 5, 10]
a = 0.01:0.01:0.05;
r = 4:20;
[X, Y, Z] = meshgrid(r, a, h);
subplot(6, 4, count)
Norm_costf=costf./max(costf)
imagesc(r,a,Norm_costf,'Interpolation', 'bilinear');
% title(sprintf('CB %d(m)',h))
shading interp
hold on
c_levels = (0:0.1:1);
[c,h] = contour(r,a,Norm_costf,c_levels,'k');
clabel(c,h,'fontsize',7)
hCB=colorbar
hCB.Label.String='Normalised Cost function';
gm=costf==min(costf(:));
hold on
scatter3(X(gm),Y(gm),Z(gm),'w*')
hold off
xlabel('CER_{ref}(\mum)')
ylabel('\alpha_{ref}(1/m)')
set(gca,'xlim',[4 20],'xtick',[4 12 20],'ylim',[0.005 0.055],'ytick',[0.005 0.025 0.055]);
xlim([4, 20])
count = count + 1;
hold on
end
end

 Risposta accettata

subplot counts across the first row first, i.e., subplot(6,4,2) is the subplot in the second column of the first row (not the first column of the second row). Therefore, I think you should swap the order of your for loops to get the subplots in the order you want. Then you can title the first four subplots (assuming you just want titles on the top ones).
figure
count = 1;
for fov = [0.2, 0.5, 1, 2, 5, 10]
for h = 1000:1000:4000
subplot(6, 4, count)
if count <= 4 % title subplots 1, 2, 3, 4
title(sprintf('CB %d(m)',h));
end
count = count + 1;
end
end
(To illustrate what happens with the order as you have it now:)
figure
count = 1;
for h = 1000:1000:4000
for fov = [0.2, 0.5, 1, 2, 5, 10]
subplot(6, 4, count)
if mod(count,6) == 1 % title plots 1, 7, 13, 19
title(sprintf('CB %d(m)',h));
end
count = count + 1;
end
end

7 Commenti

Thank you so much, Sir. Proud of you
I'm having the following error with using the code:
How can I solve it, please?
That error is because this line:
[c,h] = contour(r,a,Norm_costf,c_levels,'k');
overwrites the loop variable h. You should change it to something else, and use that when you want to refer to the contour, e.g.:
[c,hh] = contour(r,a,Norm_costf,c_levels,'k');
clabel(c,hh,'fontsize',7)
Excuse me, the code works perfectly but what I aim is to display the title CB 1000(m) upto the sixth subplot in a row till CB 2000(m) and then the same for the next title. Now the titles are displayed only on the first subplots as
figure
count = 1;
for fov = [0.2, 0.5, 1, 2, 5, 10]
for h = 1000:1000:4000
subplot(6, 4, count)
title(sprintf('CB %d(m)',h));
text(0.5,0.5,sprintf('%d',count));
count = count + 1;
end
end

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by