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

25 views (last 30 days)
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

Accepted Answer

Voss
Voss on 26 Apr 2022
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 Comments
Voss
Voss on 27 Apr 2022
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

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!