-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpubfig.m
347 lines (272 loc) · 11.3 KB
/
pubfig.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
classdef pubfig
% A class of static functions that automates figures styling for scientific publications
% pubfig.funcname(...)
properties(Constant)
pathname = '~/www/figs';
end
properties(Constant)
end
methods(Static)
function text_size(fontsize, fhnd)
% function text_size(fontsize, fhnd)
% set the text font size of all the axes in a
% given figure
if (nargin < 2)
fhnd = gcf;
end
axAllH = findobj(fhnd, 'type', 'axes').';
for h = axAllH
set(h, 'FontSize', fontsize);
set(get(h, 'Title'), 'FontSize', fontsize);
set(get(h, 'Xlabel'), 'FontSize', fontsize);
set(get(h, 'Ylabel'), 'FontSize', fontsize);
set(get(h, 'Zlabel'), 'FontSize', fontsize);
end
end
function mul_axis_tick_by_factor(factor, XYZind, ahnd)
% function mul_axis_tick_by_factor(factor, XYZind, ahnd)
% multiple axis ticks by a given factor
% XYZind is 'X' or 'Y' or 'Z'
% ahnd is the axes handle, default value is gca
if (nargin < 3)
ahnd = gca;
end
axticks = get(ahnd, [XYZind 'Tick']);
set(ahnd, [XYZind 'TickLabel'], factor*axticks);
end
function make_ax_exp(XYZind, ahnd, basis)
% function make_ax_exp(XYZind, ahnd, basis)
% make an axis exponent.
% E.g. axis changes from -2, -3, .. to '10^-2', '10^-3', ...
%
% XYZind is 'X' or 'Y' or 'Z'
% ahnd is the axes handle, default value is gca
% basis is 2, 10, 'exp'. default value is 10
if (nargin < 2)
ahnd = gca;
end
if (nargin < 3)
basis = 10;
end
switch basis
case 10
strbasis = '10';
case 2
strbasis = '2';
case 'exp'
strbasis = 'e';
end
pubfig.set_axishnd_tick_spacing(5, ahnd, XYZind, true);
axticks = get(ahnd, [XYZind 'TickLabel']);
newticks = [];
for cnt = 1:size(axticks,1)
newticks{cnt} = [strbasis '^' axticks(cnt,:)];
end
set(ahnd, [XYZind 'TickLabel'], newticks);
end
function mirror_axis( XYZind, ahnd)
% function mirror_axis( XYZind, ahnd)
% mirror axis ticks.
% E.g. 1, 2, 3, 4, 5 changes to 5, 4, 3, 2, 1
% XYZind is 'X' or 'Y' or 'Z'
% ahnd is the axes handle, default value is gca
if (nargin < 2)
ahnd = gca;
end
axticks = get(ahnd, [XYZind 'TickLabel']);
set(ahnd, [XYZind 'TickLabel'], flipud(axticks));
end
function set_axishnd_tick_spacing(nintervals, ahnd, XYZind, toround)
% function set_axishnd_tick_spacing(nintervals, ahnd, XYZind, toround)
% set the spacing between a given axis values
% XYZind is 'X' or 'Y' or 'Z'
% ahnd is the axes handle
% to round: true/false. true means use ticks with round values.
% default value is false
if (nargin < 4)
toround = false;
end
inlims = get(ahnd, [XYZind 'Lim']);
interval = (diff(inlims)/nintervals);
if(toround == true)
interval = round(interval);
end
if toround == false
set(ahnd, [XYZind 'Tick'], inlims(1):interval:inlims(2));
else
set(ahnd, [XYZind 'Tick'], floor(inlims(1)):interval:ceil(inlims(2)));
end
end
function set_axis_tick_spacing(nintervals, fhnd, toround)
% function set_axis_tick_spacing(nintervals, fhnd, toround)
% set the spacing between values of all figure axes
% fhnd is the figure handle, default is gcf
% to round: true/false. true means use ticks with round values.
% default value is false
if (nargin < 2)
fhnd = gcf;
end
if (nargin < 3)
toround = false;
end
axAllH = findobj(fhnd, 'type', 'axes').';
axLegH = findobj(fhnd, 'tag', 'legend').';
axAllH = setdiff(axAllH, axLegH); % remove the legends axes
for h = axAllH
cnt = 1;
for ax = {'X', 'Y', 'Z'}
inlims = get(h, [ax{1} 'Lim']);
interval = (diff(inlims)/nintervals);
if(toround == true)
interval = round(interval);
end
set(h, [ax{1} 'Tick'], inlims(1):interval:inlims(2));
% set(h, [ax{1} 'TickLabel'], xyzFactors(cnt)*(inlims(1):interval:inlims(2)));
cnt = cnt+1;
end
end
end
function setbox(onoff, fhnd)
% function setbox(onoff, fhnd)
% set figure box to on/off
% onoff values are 'on' or 'off'
% fhnd is the figure handle, default is gcf
if (nargin < 2)
fhnd = gcf;
end
axAllH = findobj(fhnd, 'type', 'axes').';
axLegH = findobj(fhnd, 'tag', 'legend').';
axAllH = setdiff(axAllH, axLegH); % remove the legends axes
for h = axAllH
set(h, 'Box', onoff)
end
end
function print_eps(filename, fhnd, other_format)
% function print_eps(filename, fhnd, other_format)
% prints a figure to eps file
% fhnd is the figure handle, default is gcf
%
% other_format, allows to export the eps as other format as well
% e.g. 'jpeg', 'tiff', 'png'. Default value is [].
% To use other_format, you should install eps2xxx() from
% http://www.mathworks.com/matlabcentral/fileexchange/6858-eps2xxx
% and also you need to install Ghostscript
if nargin<2
fhnd = gcf;
end
if nargin < 3
other_format = [];
end
A3sizecm = [ 14.85, 21];
pos = get(fhnd, 'Position');
pos = pos(3:4);
[mxpos, xy] = max(pos);
paperpos = [A3sizecm(xy) A3sizecm(xy)*pos(3-xy)/mxpos];
if (xy == 2)
paperpos = fliplr(paperpos);
end
set(fhnd, 'PaperUnits', 'centimeters');
set(fhnd, 'PaperPosition', [0 0 paperpos]);
print(fhnd, '-depsc2', '-r300', [filename '.eps']);
if ~isempty(other_format)
eps2xxx([filename '.eps'], other_format);
end
end
function print_fig(filename, ftype, fhnd)
% function print_fig(filename, ftype, fhnd)
% prints a figure to a specific format file
% ftype is the format type. e.g. 'jpeg', 'tiff', 'png'.
% Default value is 'tiff'
% fhnd is the figure handle, default is gcf
if nargin<3
fhnd = gcf;
end
if nargin<2
ftype = 'tiff';
end
switch ftype
case 'tiff'
ext = '.tif';
case 'jpeg'
ext = '.jpg';
otherwise
ext = ['.' ftype];
end
if nargin < 1
filename = ['figure' num2str(fhnd)];
end
if ~ispc
pubfig.print_eps(filename, fhnd, {ftype})
else
A3sizecm = [ 14.85, 21];
pos = get(fhnd, 'Position');
pos = pos(3:4);
[mxpos, xy] = max(pos);
paperpos = [A3sizecm(xy) A3sizecm(xy)*pos(3-xy)/mxpos];
if (xy == 2)
paperpos = fliplr(paperpos);
end
set(fhnd, 'PaperUnits', 'centimeters');
set(fhnd, 'PaperPosition', [0 0 paperpos]);
print(fhnd, ['-d' ftype], '-r300', [filename ext]);
end
end
function save(fname_fig, ftype, fhnd)
% function print_fig(filename, ftype, fhnd)
% prints a figure to a specific format file
% ftype is the format type. e.g. 'jpeg', 'tiff', 'png'.
% Default value is 'tiff'
% fhnd is the figure handle, default is gcf
if nargin<3
fhnd = gcf;
end
if nargin<2
ftype = 'tiff';
end
switch ftype
case 'tiff'
ext = 'tif';
case 'jpeg'
ext = 'jpg';
otherwise
ext = [ftype];
end
if nargin < 1
fname_fig = ['figure' num2str(fhnd)];
end
saveas(fhnd, fname_fig, ext);
end
function template1(fhnd)
% function template1(fhnd)
% template1 is a automatic template to run a set of styling
% command on a figure
% fhnd is the figure handle, default is gcf
%
if (nargin < 1)
fhnd = gcf;
end
pubfig.text_size(20, fhnd);
pubfig.set_axis_tick_spacing(5, fhnd);
pubfig.setbox('off', fhnd)
end
function your_own_template(fhnd)
% function your_own_template(fhnd)
% fhnd is the figure handle, default is gcf
% you are encouraged to add your own styling template
if (nargin < 1)
fhnd = gcf;
end
end
function fname_fig = titlestr_to_fname(title_str)
fname_fig = regexprep(title_str, ' ', '_' );
fname_fig = regexprep(fname_fig, ',', '' );
fname_fig = regexprep(fname_fig, '\\_', '_' );
fname_fig = regexprep(fname_fig, '\n', '_' );
end
function char = newline()
% function char = newline()
% return a newline char (sprintf('\n'))
char = sprintf('\n');
end
end
end