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

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • 2
    Try the `suplabel` submission on MATLAB FEX: http://www.mathworks.com/matlabcentral/fileexchange/7772-suplabel – rayryeng May 03 '16 at 20:26

3 Answers3

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!

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Amal
  • 396
  • 1
  • 6
  • 2
    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. – Amal May 03 '16 at 21:21
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.

horchler
  • 18,384
  • 4
  • 37
  • 73
-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.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Sergey Sigaev
  • 100
  • 2
  • 10
  • 2
    The OP wants an **overall** title over the figure. This labels all subplots individually. Please read the question carefully. – rayryeng May 03 '16 at 20:42
  • 1
    In fact, my decision is wrong, sorry. – Sergey Sigaev May 03 '16 at 21:02
  • 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. – rayryeng May 03 '16 at 21:02