-
Notifications
You must be signed in to change notification settings - Fork 2
/
fig.m
261 lines (227 loc) · 6.94 KB
/
fig.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
function h = fig(varargin)
% FIG - Creates a figure with a desired size, no white-space, and several other options.
%
% All Matlab figure options are accepted.
% FIG-specific options of the form FIG('PropertyName',propertyvalue,...)
% can be used to modify the default behavior, as follows:
%
% -'units' : preferred unit for the width and height of the figure
% e.g. 'inches', 'centimeters', 'pixels', 'points', 'characters', 'normalized'
% Default is 'centimeters'
%
% -'width' : width of the figure in units defined by 'units'
% Default is 14 centimeters
% Note: For IEEE journals, one column wide standard is
% 8.5cm (3.5in), and two-column width standard is 17cm (7 1/16 in)
%
% -'height' : height of the figure in units defined by 'units'
% Specifying only one dimension sets the other dimension
% to preserve the figure's default aspect ratio.
%
% -'font' : The font name for all the texts on the figure, including labels, title, legend, colorbar, etc.
% Default is 'Times New Roman'
%
% -'fontsize' : The font size for all the texts on the figure, including labels, title, legend, colorbar, etc.
% Default is 14pt
%
% -'border' : Thin white border around the figure (compatible with export_fig -nocrop)
% 'on', 'off'
% Default is 'off'
%
% FIG(H) makes H the current figure.
% If figure H does not exist, and H is an integer, a new figure is created with
% handle H.
%
% FIG(H,...) applies the properties to the figure H.
%
% H = FIG(...) returns the handle to the figure created by FIG.
%
%
% Example 1:
% fig
%
% Example 2:
% h=fig('units','inches','width',7,'height',2,'font','Helvetica','fontsize',16)
%
%
% Copyright © 2012 Reza Shirvany, [email protected]
% Source: http://www.mathworks.com/matlabcentral/fileexchange/30736
% Updated: 05/14/2012
% Version: 1.6.5
%
% default arguments
width=28;
font='Times New Roman';
fontsize=14;
units='pixels';
bgcolor='w';
sborder='off';
flag='';
Pindex=[];
%%%%%%%%%%% process optional arguments
optargin = size(varargin,2);
if optargin>0
% check if a handle is passed in
if isscalar(varargin{1}) && isnumeric(varargin{1})
flag=[flag '1'];
i=2;
if ishghandle(varargin{1})==1
flag=[flag 'i'];
end
else
i=1;
end
% get the property values
while (i <= optargin)
if (strcmpi(varargin{i}, 'border'))
if (i >= optargin)
error('Property value required for: %s', num2str(varargin{i}));
else
sborder = varargin{i+1};flag=[flag 'b'];
i = i + 2;
end
elseif (strcmpi(varargin{i}, 'width'))
if (i >= optargin)
error('Property value required for: %s', num2str(varargin{i}));
else
width = varargin{i+1};flag=[flag 'w'];
i = i + 2;
end
elseif (strcmpi(varargin{i}, 'height'))
if (i >= optargin)
error('Property value required for: %s', num2str(varargin{i}));
else
height = varargin{i+1};flag=[flag 'h'];
i = i + 2;
end
elseif (strcmpi(varargin{i}, 'font'))
if (i >= optargin)
error('Property value required for: %s', num2str(varargin{i}));
else
font = varargin{i+1};flag=[flag 'f'];
i = i + 2;
end
elseif (strcmpi(varargin{i}, 'fontsize'))
if (i >= optargin)
error('Property value required for: %s', num2str(varargin{i}));
else
fontsize = varargin{i+1};flag=[flag 's'];
i = i + 2;
end
elseif (strcmpi(varargin{i}, 'units'))
if (i >= optargin)
error('Property value required for: %s', num2str(varargin{i}));
else
units = varargin{i+1};flag=[flag 'u'];
i = i + 2;
end
elseif (strcmpi(varargin{i}, 'color'))
if (i >= optargin)
error('Property value required for: %s', num2str(varargin{i}));
else
bgcolor = varargin{i+1};flag=[flag 'c'];
i = i + 2;
end
else
%other figure properties
if (i >= optargin)
error('A property value is missing.');
else
Pindex = [Pindex i i+1];
i = i + 2;
end
end
end
end
% We use try/catch to handle errors
try
% creat a figure with a given (or new) handle
if length(strfind(flag,'1'))==1
s=varargin{1};
if ishandle(s)==1
set(0, 'CurrentFigure', s);
else
figure(s);
end
else
s=figure;
end
flag=[flag 's'];
% set other figure properties
if ~isempty(Pindex)
set(s,varargin{Pindex});
end
% set the background color
set(s, 'color',bgcolor);
% set the font and font size
set(s, 'DefaultTextFontSize', fontsize);
set(s, 'DefaultAxesFontSize', fontsize);
set(s, 'DefaultAxesFontName', font);
set(s, 'DefaultTextFontName', font);
%%%%%%%%%%% set the figure size
% set the root unit
old_units=get(0,'Units');
set(0,'Units',units);
% get the screen size
scrsz = get(0,'ScreenSize');
% set the root unit to its default value
set(0,'Units',old_units);
% set the figure unit
set(s,'Units',units);
% get the figure's position
pos = get(s, 'Position');
old_pos=pos;
aspectRatio = pos(3)/pos(4);
% set the width and height of the figure
if length(strfind(flag,'w'))==1 && length(strfind(flag,'h'))==1
pos(3)=width;
pos(4)=height;
elseif isempty(strfind(flag,'h'))
pos(3)=width;
pos(4) = width/aspectRatio;
elseif isempty(strfind(flag,'w')) && length(strfind(flag,'h'))==1
pos(4)=height;
pos(3)=height*aspectRatio;
end
% make sure the figure stays in the middle of the screen
diff=old_pos-pos;
if diff(3)<0
pos(1)=old_pos(1)+diff(3)/2;
if pos(1)<0
pos(1)=0;
end
end
if diff(4)<0
pos(2)=old_pos(2)+diff(4);
if pos(2)<0
pos(2)=0;
end
end
% warning if the given width (or height) is greater than the screen size
if pos(3)>scrsz(3)
warning(['Maximum width (screen width) is reached! width=' num2str(scrsz(3)) ' ' units]);
end
if pos(4)>scrsz(4)
warning(['Maximum height (screen height) is reached! height=' num2str(scrsz(4)) ' ' units]);
end
% apply the width, height, and position to the figure
set(s, 'Position', pos);
if strcmpi(sborder, 'off')
set(s,'DefaultAxesLooseInset',[0,0,0,0]);
end
% handle errors
catch ME
if isempty(strfind(flag,'i')) && ~isempty(strfind(flag,'s'))
close(s);
end
error(ME.message)
end
s=figure(s);
% return handle if caller requested it.
if (nargout > 0)
h =s;
end
%
% That's all folks!
%
%flag/1iwhfsucsb