-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.m
335 lines (298 loc) · 12.8 KB
/
Graphics.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
% Created by Jorge M. Cruz-Duarte, feb-2018
% Contact: [email protected]
%
% Example:
% >> q = Graphics('Hello_World'); % Create the object
%
% >> plot(rand(1,3), rand(1,3),'b'), hold on,
% >> plot(rand(1,3), rand(1,3),'r'),
% >> xlabel('x-axis'); ylabel('y-axis');
% >> legend('Data 1','Data 2');
%
% >> setup(q); % Quick setup
% >> save(q); % Print graphic
%
classdef Graphics
properties
objID
fileName
end
methods
%% Construct an object and initialise it
function obj = Graphics(namestring)
if nargin > 0
if ischar(namestring)
% Crate the object
obj.fileName = namestring;
obj.objID = figure('color','w','name',namestring);
% Set default properties
setup(obj);
else
error('Name must be char!')
end
else
[~,namestring] = fileparts(tempname);
obj = Graphics(namestring);
end
end
%% Destroy an object
function kill(obj)
if nargin > 0
close(obj.objID);
else
error('An object is needed!!!');
end
end
%% Save the graphic as an eps figure
function save(obj,otherName,extFile)
delim = '_';
if nargin < 3
extFile = '.eps';
if nargin < 2
otherName = '';
delim = '';
end
end
if strcmp(otherName,'')
name = obj.fileName;
else
name = [obj.fileName,delim,otherName];
end
switch extFile
case '.jpg'
print(obj.objID,[name,extFile],'-r333','-djpeg','-noui');
case '.png'
print(obj.objID,[name,extFile],'-r333','-dpng','-noui');
otherwise
print(obj.objID,[name,extFile],'-painters','-depsc','-tiff','-noui');
end
end
%% Setting some properties of the graphics
function setsize(obj,columns,aspectRatio)
if nargin > 0
% Get paper size
paperSize = [21.59,27.94]; % in cm %get(obj.objID,'PaperSize');
% Set margins
margin = 2; % in cm
columnsep = 0.35; % in cm
% textFullWidth
textFullWidth = paperSize(2) - 2*margin;
warningNargin1 = 'Nothing was set! Introduce an object';
if nargin > 1
% Read Columns
if columns > 1 %&& mod(columns,1) == 0
lineWidth = textFullWidth/columns - columnsep/2;
else
lineWidth = textFullWidth;
end
% Read aspectRatio
errorReadAR.message = ['Aspect ratio must be char, ',...
'number or 2D vector. For example: ',...
'''4:2 (default)'', ','0.25, or [12,4]'];
errorReadAR.identifier = 'Graphics:aspectRatioRead';
if ischar(aspectRatio)
asprs = split(aspectRatio,':');
if numel(asprs) ~= 2, error(errorReadAR); end
alpha = 1/eval(sprintf('%s/%s',asprs));
elseif isnumeric(aspectRatio)
switch numel(aspectRatio)
case 1
alpha = aspectRatio;
case 2
alpha = aspectRatio(2)/aspectRatio(1);
otherwise
error(errorReadAR);
end
else
error(errorReadAR);
end
% Set figure size
graphicSize = lineWidth*[1,alpha]; % width, height in cm
% set(obj.objID,'Menubar','none');
set(obj.objID,'PaperUnits','centimeters');
set(obj.objID,'PaperSize',graphicSize);
set(obj.objID,'PaperPosition',[0 0 graphicSize]);
set(obj.objID,'Units','centimeters',...
'Position',[0 0 graphicSize]);
movegui(obj.objID, 'center');
else % Default set size: columns = 1, aspectRatio = 2:1
setsize(obj,1,[2,1]);
end
else
warning(warningNargin1)
end
end
%% Setting font size
function setfont(obj,fontSize)
if nargin > 0
if nargin > 1
% Check fontSize
errorReadFS.message = ['Font size must be number',...
' in pt. For example: 10.5 or 12 (default)'];
errorReadFS.identifier = 'Graphics:fontSizeRead';
if ~(isnumeric(fontSize) && numel(fontSize) == 1)
error(errorReadFS);
end
% Set fonts
if ~isempty(obj.objID.CurrentAxes)
% Find all axes
allAxes = findall(obj.objID,'type','axes');
for ia = 1 : numel(allAxes)
ax = allAxes(ia);
set(ax,'FontSize',fontSize);
set(ax,'TickLabelInterpreter','latex');
set(ax.XLabel,'Interpreter','latex');
set(ax.YLabel,'Interpreter','latex');
set(ax.ZLabel,'Interpreter','latex');
if ~isempty(ax.Legend)
set(ax.Legend,'FontSize',fontSize);
set(ax.Legend,'Interpreter','latex');
end
end
end
else
setfont(obj,12)
end
else
warning(warningNargin1);
end
end
%% Set line
function setline(obj,lineWidth)
if nargin > 1
errorReadLW.message = ['Line width must be number',...
' in pt. For example: 0.5 or 1 (default)'];
errorReadLW.identifier = 'Graphics:fontSizeRead';
if ~(isnumeric(lineWidth) && numel(lineWidth) == 1)
error(errorReadLW);
end
if ~isempty(obj.objID.Children)
% Find all axes
allAxes = findall(obj.objID,'type','axes');
for ia = 1 : numel(allAxes)
ax = allAxes(ia);
set(ax,'LineWidth',lineWidth);
end
end
else
setline(obj,1)
end
end
%% quick setup
function setup(obj)
setsize(obj);
setfont(obj);
setline(obj);
TextEPS = [
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 1 1 1 1 1 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0 1 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0 0
0 0 0 1 0 0 1 1 1 1 1 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0
1 1 1 1 0 0 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1
0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
1 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0
0 1 0 1 0 1 0 0 0 0 1 0 0 1 1 1
0 0 1 1 1 0 0 0 0 0 1 0 0 0 0 1
1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1
];
TextJPG(TextJPG == 0) = nan;
CDataEPS = nan(16,16,3);
CDataEPS(:,:,1) = TextEPS;
CDataPNG = nan(16,16,3);
CDataPNG(:,:,1) = TextPNG;
CDataPNG(:,:,3) = TextPNG;
CDataJPG = nan(16,16,3);
CDataJPG(:,:,1) = 0.2*TextJPG;
CDataJPG(:,:,3) = TextJPG;
% Put a uibutton for print
if isempty(findall(obj.objID,'Tag','.eps'))
uipushtool(findall(obj.objID,'Type','uitoolbar'),...
'CData',CDataEPS,...
'TooltipString','Save to EPS', 'Tag', '.eps', ...
'Separator','on','HandleVisibility','off',...
'ClickedCallback',@pushbutton1_Callback);
end
if isempty(findall(obj.objID,'Tag','.png'))
uipushtool(findall(obj.objID,'Type','uitoolbar'),...
'CData',CDataPNG,...
'TooltipString','Save to PNG', 'Tag', '.png', ...
'Separator','on','HandleVisibility','off',...
'ClickedCallback',@pushbutton1_Callback);
end
if isempty(findall(obj.objID,'Tag','.jpg'))
uipushtool(findall(obj.objID,'Type','uitoolbar'),...
'CData',CDataJPG,...
'TooltipString','Save to JPG', 'Tag', '.jpg', ...
'Separator','on','HandleVisibility','off',...
'ClickedCallback',@pushbutton1_Callback);
end
% ButtonH = uicontrol('Parent',obj.objID,'Style','Pushbutton',...
% 'String','Print','Units','Normalized',...
% 'Position',[0.88 0.9 0.11 0.07],'Visible','on',...
% 'Callback',@pushbutton1_Callback);
% function Pushing()
% [~,RandomEnding] = fileparts(tempname);
% ButtonH.Visible = 'off';
% save(gcf,RandomEnding(end-11:end));
% ButtonH.Visible = 'on';
% end
function pushbutton1_Callback(hObject, eventdata, handles)
fprintf('Saving...\n');
% hObject.Tag = .eps | .jpg | .png
save(obj,'',hObject.Tag);
fprintf('Saved as ''%s%s''\n',obj.fileName,hObject.Tag);
end
end
% set all up
function setall(obj,columns,aspectratio,font,line)
setup(obj);
setsize(obj,columns,aspectratio);
setfont(obj,font);
setline(obj,line);
end
end
end % class