Question

An example of subplots is given here:

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)

How would one add labels to the subplots in this example? Just adding 'figure 1' 'figure 2' etc would be instructive.

Was it helpful?

Solution 2

Add two lines at the end of the script like this:

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)

OTHER TIPS

I think many people will come across this entry in order to find a way to simply add a title to a subplot without any copying (as I did). For this case, it can be done very easily as already stated by 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

The important part here is (and I think that's where most of the errors come from) that the title-command must come after the actual plot command. If it was written before plotting the graph, the title would not appear!

Can you not use

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

at the end of the script? Or have i missed something?

Alternatively use title in the original figures e.g.

 figure(1)
 surf(peaks(10))
 title('first figure')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top