How can I change the title of the subplots with forloop iteration?
4 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/979665/image.png)
%% 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
0 个评论
采纳的回答
Voss
2022-4-26
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 个评论
Voss
2022-4-27
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!