1

这里给出了一个子图的例子:

http://www.mathworks.com/support/solutions/en/data/1-16BSF/?product=SL&solution=1-16BSF

figure(1)
surf(peaks(10))
colorbar

figure(2)
mesh(peaks(10))
colorbar

figure(3)
contour(peaks(10))
colorbar

figure(4)
pcolor(peaks(10))
colorbar

% Now create destination graph

figure(5)
ax = zeros(4,1);
for i = 1:4
ax(i)=subplot(4,1,i);
end

% Now copy contents of each figure over to destination figure
% Modify position of each axes as it is transferred

for i = 1:4
figure(i)
h = get(gcf,'Children');
newh = copyobj(h,5)
for j = 1:length(newh)
posnewh = get(newh(j),'Position');
possub = get(ax(i),'Position');
set(newh(j),'Position',...
[posnewh(1) possub(2) posnewh(3) possub(4)])
end
delete(ax(i));
end
figure(5)

在此示例中,如何将标签添加到子图中?只需添加“图 1”“图 2”等即可。

4

3 回答 3

4

我想很多人会遇到这个条目,以便找到一种方法来简单地将标题添加到子情节而无需任何复制(就像我所做的那样)。对于这种情况,正如 Sanjay Manohar 所述,它可以很容易地完成:

figure(1)

subplot(4,1,1)
surf(peaks(10))
title('Figure 1') % must come AFTER the plot command
colorbar

subplot(4,1,2)
mesh(peaks(10))
title('Figure 2') % must come AFTER the plot command
colorbar

subplot(4,1,3)
contour(peaks(10))
title('Figure 3') % must come AFTER the plot command
colorbar

subplot(4,1,4)
pcolor(peaks(10))
title('Figure 4') % must come AFTER the plot command
colorbar

这里的重要部分是(我认为这是大多数错误的来源)title-command 必须在实际的 plot 命令之后。如果是在绘制图形之前写的,标题就不会出现!

于 2014-12-04T09:21:39.957 回答
1

可以不使用吗

figure(5)
subplot(4,1,1)
title('first figure')
subplot(4,1,2)
...

在脚本的末尾?还是我错过了什么?

或者title在原始图中使用,例如

 figure(1)
 surf(peaks(10))
 title('first figure')
于 2013-07-29T15:43:00.387 回答
1

在脚本末尾添加两行,如下所示:

string = {'Figure 1','Figure 2','Figure 3','Figure 4'}; %%% or any titles you want
for i = 1:4
figure(i)
title(string{i}) %%% add this line
h = get(gcf,'Children');
newh = copyobj(h,5)
for j = 1:length(newh)
posnewh = get(newh(j),'Position');
possub = get(ax(i),'Position');
set(newh(j),'Position',...
[posnewh(1) possub(2) posnewh(3) possub(4)])
end
delete(ax(i));
end
figure(5)
于 2013-07-29T15:56:19.513 回答