How is it possible that Matlab still hasn't created a built in function that automatically inserts a title above a group of subplots?

32 views (last 30 days)
It is 2017, and there still is no easy single command built in function that inserts a title above a group of subplots, even though a Million of users keep asking how to do that. How is that possible?

Answers (2)

Cedric
Cedric on 25 Sep 2017
When you start getting picky, you don't use SUBPLOT anymore because it is not that flexible. Instead you create an axes object that covers the full extent and use it for global titles/legends/etc, and you place your other axes by computing their position.
figure( 'Units', 'normalized', 'Position', [0.2, 0.2, 0.6, 0.6], ...
'Color', 'white' ) ;
% - Bg axes and main title.
bgAxes = axes( 'Position', [0, 0, 1, 1], 'XColor', 'none', 'YColor', 'none', ...
'XLim', [0, 1], 'YLim', [0, 1] ) ;
text( 0.5, 0.95, 'A Small Example', 'FontSize', 16, ...
'HorizontalAlignment', 'center', 'FontWeight', 'bold' ) ;
% - Positions.
x = linspace( 0.05, 0.6, 4 ) ;
w = 0.8 * diff( x(1:2) ) ;
y = linspace( 0.07, 0.7, 3 ) ;
h = 0.8 * diff( y(1:2) ) ;
% - Headers for array still in bgAxes.
for colId = 1 : numel( x )-1
text( x(colId)+w/2, 0.03, sprintf( 'Label X%d', colId ), ...
'HorizontalAlignment', 'center', 'FontWeight', 'bold' ) ;
end
for rowId = 1 : numel( y )-1
text( 0.02, y(rowId)+h/2, sprintf( 'Label Y%d', rowId ), ...
'HorizontalAlignment', 'center', 'Rotation', 90, 'FontWeight', 'bold' ) ;
end
% - Array of plots.
for colId = 1 : numel( x )-1
for rowId = 1 : numel( y )-1
axes( 'Position', [x(colId), y(rowId), w, h] ) ;
plot( sin( rand(1) * (1:10)), 'b' ) ;
grid( 'on' ) ;
end
end
% - Surface.
axes( 'Position', [0.65, 0.45, 0.3, 0.45] ) ;
[X, Y] = meshgrid( -5: .5 : 5 ) ;
Z = Y.*sin(X) - X.*cos(Y) ;
s = surf(X,Y,Z,'FaceAlpha',0.5) ;
s.EdgeColor = 'none';
% - Time series
axes( 'Position', [0.05, 0.72, 0.515, 0.15] ) ;
x = linspace( 0, 10, 100 ) ;
plot( rand(size(x)) + 3 * sin(x) .* exp(-x/5), 'r' ) ;
xlabel( 't [s]' ) ;
ylabel( 'A [V]' ) ;
grid( 'on' ) ;
% - Barchart.
axes( 'Position', [0.65, 0.05, 0.3, 0.3] ) ;
histogram( randn(1000, 1) ) ;
set( gca, 'Box', 'off' ) ;
  3 Comments
Cedric
Cedric on 26 Sep 2017
Edited: Cedric on 26 Sep 2017
Well, why getting nuts when you could just get picky instead? ;)
More seriously, I agree with you, there should be a builtin for this. In the mean time, you could take 3 minutes for building a function out of Jan's code (that passes varargin{:} to uicontrol so you can add project-specific arguments) and store it somewhere in MATLAB path.

Sign in to comment.


Jan
Jan on 25 Sep 2017
Edited: Jan on 26 Sep 2017
Calm down.
I'm using text() or uicontrol('Style', 'Text') for this purpose. This is fast and easy. I'm not one of your million of users.
uicontrol('Style', 'Text', 'Units', 'Normalized', ...
'Position', [0.3, 0.95, 0.4,0.05], ...
'String', 'Super Title')
[EDITED] If want a function:
function H = SubTitle(String, varargin)
H = uicontrol('Style', 'Text', 'Units', 'Normalized', ...
'Position', [0.3, 0.95, 0.4,0.05], ...
'String', String, ...
varargin{:});
end
Now you can append all parameter/value pairs, which are accepted by the uicontrol command: Other positions, font size, etc.
  3 Comments
Adam
Adam on 26 Sep 2017
Why do you have to copy and paste it every time you need it?! You create the function and then you have a function. Whether it is a builtin or one you write yourself makes no difference then.
I have thousands of self-written functions to do little tasks within the repository I use. That is the whole point of a programming language, if something doesn't exist you just program it yourself and in a function so you can re-use it!

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!