-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfigtitle.m
133 lines (122 loc) · 2.95 KB
/
figtitle.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
function [ fth ] = figtitle(titlestring,varargin)
% FIGTITLE creates a title centered at the top of a figure. This may be used
% to add a title to a figure with several subplots.
%
%
%% Syntax
%
% figtitle('TitleString')
% figtitle('TitleString','TextProperty','TextValue')
% h = figtitle(...)
%
%
%% Description
%
% figtitle('TitleString') centers a title at the top of a figure and sets
% the figure name to TitleString.
%
% figtitle('TitleString','TextProperty',TextValue) formats the title with
% property name value pairs (e.g., 'FontSize',20)
%
% h = figtitle(...) returns a handle of the newly-created title.
%
%% EXAMPLE 1:
%
% x = 1:.01:7;
% y = sin(x);
%
% figure;
% subplot(2,2,1)
% plot(3*x,y)
% title('Exp. 1')
%
% subplot(2,2,2)
% plot(x,2*y+x)
% title('Exp. 2')
%
% subplot(2,2,3)
% plot(x,y)
% title('Exp. 3')
%
% subplot(2,2,4)
% plot(x,2*y)
% title('Exp. 4')
%
% figtitle('My Experimental Results','fontweight','bold');
%
%% EXAMPLE 2: A prettier example using ntitle:
%
% x = 1:.01:7;
% y = sin(x);
%
% figure;
% subplot(2,2,1)
% plot(3*x,y)
% ntitle('experiment 1','fontsize',12)
% box off
%
% subplot(2,2,2)
% plot(x,2*y+x)
% ntitle('experiment 2','fontsize',12)
% box off
%
% subplot(2,2,3)
% plot(x,-y+5*x)
% ntitle('experiment 3','fontsize',12)
% box off
%
% subplot(2,2,4)
% plot(x,2*y-3*x)
% ntitle('experiment 4','fontsize',12);
% box off
%
% figtitle(' My Experimental Results')
%
% * * * * * * * * * * * * * * * * * * * * * * * * * * * * * %
%
% In many cases a figure title may overlap a subplot title
% To reduce the possibility of a figure title overlapping subplot
% titles, try pairing this function with the ntitle function, which
% is available on the Mathworks File Exchange here:
% http://www.mathworks.com/matlabcentral/fileexchange/42114-ntitle
%
%
% * * * * * * * * * * * * * * * * * * * * * * * * * * * * * %
% Written by Chad A. Greene of the University of Texas at Austin
% Institute for Geophysics, July 2013.
%
% Updated August 2014 to include support for invisible figures
% and now also sets the figure name to the title string.
%
% * * * * * * * * * * * * * * * * * * * * * * * * * * * * * %
%
% See also title, text, and ntitle.
% Get the handle of the current axes and properties:
hca = gca;
fontsize = get(hca,'fontsize');
% Create a new set of axes the size of the entire figure:
h = axes('position',[0 0 1 1],'units','normalized');
axes('Units','normalized',...
'Position',[0 0 1 1],...
'Visible','off',...
'XTick',[],...
'YTick',[],...
'Box','off');
% Make a title:
fth = text(.5,1,titlestring,...
'units','normalized',...
'horizontalalignment','center',...
'verticalalignment','top',...
'fontsize',fontsize+2);
% Set optional inputs:
if nargin>1
set(fth,varargin{:});
end
% Now go back to from where we came:
delete(h)
set(gcf,'CurrentAxes',hca,'name',titlestring);
% Return the title handle only if it is desired:
if nargout==0
clear fth;
end
end