How to give a combined title for subplots? [duplicate]
Asked Answered
S

3

5

I want to give a combined title to my subplots, instead of titling each plot individually. e.g;

for pl=1:4
      subplot(2,2,pl)
      title('Test') 
end

gives me this:enter image description here

If I use this:

figure
title('Test') 
for pl=1:4
      subplot(2,2,pl)

end

I don't get any title.

I want my output like the following:enter image description here

Slam answered 3/5, 2016 at 20:16 Comment(1)
Try the suplabel submission on MATLAB FEX: mathworks.com/matlabcentral/fileexchange/7772-suplabelFur
M
8

There is a small trick. You can do the following to spawn a figure with multiple subplots.

h = figure 
for pl=1:4
    subplot(2,2,pl)
end

After this you have to set the NextPlot property to 'add'. Do this:

h.NextPlot = 'add';
a = axes; 

%// Set the title and get the handle to it
ht = title('Test');

%// Turn the visibility of the axes off
a.Visible = 'off';

%// Turn the visibility of the title on
ht.Visible = 'on';

Hope this helps!

Michalemichalski answered 3/5, 2016 at 20:39 Comment(1)
Since you have already defined a handle to the figure. You should use that. Use h.Name = 'Finite Horizon'. If you use figure again, of course it will open another figure window.Michalemichalski
H
3

If you have the Bioinformatics toolbox you can use suptitle. Otherwise, there's the excellent suplabel on the MathWorks File Exchange that can do this and more.

Holliman answered 3/5, 2016 at 21:26 Comment(0)
P
-3

This is my version of the solution, print this in Command Window in Matlab:

clear all
close all
clc
name={'first', 'second', 'third', 'fourth'};
for k = 1:4
    subplot(2,2,k);
    title(name(k));
end

Result

I hope this helps. Best regards.

Promenade answered 3/5, 2016 at 20:36 Comment(3)
The OP wants an overall title over the figure. This labels all subplots individually. Please read the question carefully.Fur
In fact, my decision is wrong, sorry.Promenade
It isn't a problem. Next time, make sure you read the question before answering. You can see what the expected output is from the OP and what you have provided does not match.Fur

© 2022 - 2024 — McMap. All rights reserved.