-
Notifications
You must be signed in to change notification settings - Fork 0
/
figP.m
511 lines (467 loc) · 16.3 KB
/
figP.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
function figP(filename, ext, TNR, valArgCopy, valOpenFolder, valOpenFile, valExportPdf, valOverwrite, valSkipSaveAs, valFileName)
% For internal library use only. Last function of 3 base functions. Call stack is figPSW -> figPW -> figP
if(nargin<3) TNR = false; end
if(nargin<4) valArgCopy = false; end
if(nargin<5) valOpenFolder = false; end
if(nargin<6) valOpenFile = false; end
if(nargin<7) valExportPdf = false; end
if(nargin<8) valOverwrite = false; end
if(nargin<9) valSkipSaveAs = false; end
% if(nargin<10) valFileName = ""; end
tmp = char(ext);
if(tmp(1) ~= '.')
ext = strcat('.', ext);
end
h=gcf;
% if(isempty(fileName))
fileNameExt = strcat(filename, ext);
% else
% fileNameExt = strcat(fileName, ext);
% end
if(valOverwrite)
if(exist(fileNameExt, 'file'))
delete(fileNameExt);
end
end
if(~valSkipSaveAs)
if(ext == ".pdf")
exportgraphics(gcf, fileNameExt,'ContentType','vector',...
'BackgroundColor','none')
fprintf(1, ['\t* Zapisano wektorowy rysunek: "%s%s"\n'], filename, ext);
elseif(ext == ".tif")
exportgraphics(gcf, fileNameExt, 'BackgroundColor','white')
fprintf(1, ['\t* Zapisano rysunek: "%s%s"\n'], filename, ext);
else
saveas(h, fileNameExt);
fprintf(1, ['\t* Zapisano rastrowy rysunek: "%s%s"\n'], filename, ext);
end
end
path = fileNameExt;
if valExportPdf
path = strcat(filename, ".pdf");
end
% noBrowserOrPhoneRunning = checkSystem();
if(~inBrowserOrMobileRuned)
if valOpenFolder
if ismac
[status, results] = system((strcat('open -R "', string(path), '"')));
else
if(ispc)
[filepath,name,ext] = fileparts(path);
% winopen(filepath) % open without select file
command = char(strcat("explorer.exe /select,", '"',strcat(pwd,"\",fullfile(filepath,strcat(name,ext))),'"'));
dos(command);
else
if(isunix)
x=strcat("xdg-open ",pwd,"/",fileparts(path));
system(x);
else
warning("Unknown Operating System. Open folder param skiped")
end
end
end
end
if valOpenFile
finder(char(path))
end
else
if(valOpenFile) fprintf(1,"\t- Skipped open file, probably you launched scripts in browser or on smartphone"); end
if(valOpenFolder) fprintf(1,"\t- Skipped open folder, probably you launched scripts in browser or on smartphone"); end
end
return
% if file exist timestamp add
% "figPW(ext = 'png', FigType = 0 do 4, katalog = 'figury/')",...
%0 fast
% overwrite flag
% "figPW('svg', 4, 'figury/')"); -2 dbstack? if figPSW
if( nargin==1 && length(dbstack(1)) == 0 ) % if only one param & not runed from console
fprintf('%s\n%s\n',...
"figPW(ext = 'png', FigType = 0 do 4, katalog = 'figury/')",...
"figPW('svg', 4, 'figury/')");
end
% parse multiple arg
% addtimestamp TODO
if(nargin<1) FigType=0; end; %high DPI PNG
if(nargin<2) ext ='png'; end;
if(nargin<3) katalog = 'figury/'; end;
variantHorizontalLegend = 0;
% if FigType=="help"
% disp "help willbe herae What you want to know?";
%parse
%figtype
if(strcmpi(FigType,"nomargin")) FigType=4; end
if(strcmpi(FigType,"maximize")) FigType=5; end
%ext - optional
if ~exist(katalog, 'dir') mkdir (katalog); end;
fTitle = ''; % do zapisywanej nazwy pliku
h1=get(gca,'title');
handle = gcf;
tmp = handle.Children;
SGtitle = '';
try
for i = 1:length(tmp)
tmp = handle.Children(i);
if(strcmp(get(tmp, 'type'), 'subplottext') == 1 )
SGtitle = tmp.String;
end
end
catch
disp('Error getting sgtitle')
end
if( isempty(SGtitle) )
title=get(h1,'string');
if( isempty(title) )
if(nargin<1)
else
fTitle = [ datestr(now ,'yyyy-mm-dd_HH.MM.ss') '_'];
end
else
fTitle = strcat(title, "_");
end
else
% tmp = tmp.String;
fTitle = strcat(SGtitle, "_");
end
nrName = get( get(gcf,'Number'), 'Name' );
% title([{'Dziedzina czasu'},{'Cha-ka skokowa'}]);
% xlabel('O rzeczywista'); ylabel('O urojona');
% legend(str{:});
[calling_mfile, index] = dbstack(0);
mcName = [calling_mfile(length(calling_mfile)).name '_'];
[path, filename, Fext] = fileparts( mfilename('.')); [~, folderName] = fileparts(pwd()); folderName = strcat(katalog, folderName, "_");%if(nargin == 1) folderName = katalog; nrName=''; end; % nazwa TEGO *.m-pliku
filename = strcat(mcName, fTitle, nrName, num2str(get(gcf,'Number')));
filename = regexprep(filename, {'[%()*:\\" / ]+', '_+$'}, {'_', ''});
if(size(filename,1) > 1) filename = filename(1);end;
filename = strcat(folderName,filename);
if(ext(1) ~= '.')
ext = strcat('.', ext);
end
filenameExt = strcat(filename, ext); % Default filename
FigType
if(nargin<=3&&FigType==0)
print( filenameExt, '-dpng', '-r300'); % Zapisz jako tenMPlik_nrOstatniejFigury.png
fprintf("\t*%s\n", strcat("Zapisano: ", filenameExt));
if(nargin==1)
copygraphics(gcf);
end
return % jeżeli mniej niż 2 parametry
end
h=gcf; % for saveas()
% set(h,'PaperOrientation','Landscape');
%set(h,'Units','centimeters');
%set(h,'OuterPosition',[1, 1, 29 21]);
% set(h,'Position',2*get(h,'Position'));
% fn = nextname(filename, int2str(nrKol), "") to do
defaultExtension = '.fig';
if( strcmpi(ext, defaultExtension) == 0 ) % case insensitive
if(defaultExtension(1) ~= '.') defaultExtension = strcat('.', defaultExtension); end
saveas(h, strcat(filename, defaultExtension));
fprintf(1, ['\t* Zapisano rysunek "%s%s"\n'], filename, defaultExtension);
end
% zapiszFig(nrPliku, nrKol, 'fig');
% zapiszFig(nrPliku, nrKol, 'pdf');
% zapiszFig(nrPliku, nrKol, 'emf'); %!
% zapiszFig(nrPliku, nrKol, 'eps');
% zapiszFig(nrPliku, nrKol, 'tif');
% zapiszFig(nrPliku, nrKol, 'pcx');
% zapiszFig(nrPliku, nrKol, 'jpg');
% zapiszFig(nrPliku, nrKol, 'pbm');
% zapiszFig(nrPliku, nrKol, 'pgm');
% zapiszFig(nrPliku, nrKol, 'png');
% zapiszFig(nrPliku, nrKol, 'ppm');
if( ~exist('FigType', 'var') ) FigType = 1; end;
LineWidth = 0.75; % było 0.5
GridAlpha = 0.75; % procentowo
%LineStyle=['-'];
MarkerSize = 3;
FontSizeTitle = 9;
FontSizeLabels = 9;
FontSizeTicks = 8;
FontSizeLegend = 8;%FontSizeLegend = 16;
Resolution = 1200; % 150, 300, 600, 1200
FigX0 = 2; FigY0 = 2; % (left,down) corner of the window
FigWidthShort = 5.75; % window X size (in centimeters) - all = fig + text
FigWidthLong = 2*5.75; % window X size (in centimeters) - all = fig + text
FigHeight = 4.25; % window Y size 4.25
PosFigLong = [.18/2 .2 .89 .690]; % Dell % PosFigLong = [.095 .20 .87 .68]; % Asus
%PosFigShort = [.175 .2 .775 .690]; % Dell % [.175 .2 .775 .725]; % Dell PosFigShort = [.195 .205 .775 .68]; % Asus
PosFigShort = [.175 .195 .775 .690]; % Dell % [.175 .2 .775 .725]; % Dell PosFigShort = [.195 .205 .775 .68]; % Asus
%PosFigShort = [.15 .2 .775 .690]; % Dell % [.175 .2 .775 .725]; % Dell PosFigShort = [.195 .205 .775 .68]; % Asus
font = 'Times'; % 'Times';
if( FigType==1 ) % 1 long figure
FigWidth = FigWidthLong; PosFig = PosFigLong;
end
if( FigType==2 ) % 2 short figures, side by side
FigWidth = FigWidthShort; PosFig = PosFigShort;
end
if( FigType==3 ) % 3 as is on monitor
set(gcf,'Units','centimeters')
PosFig = get(gcf,'Position'); FigWidth = PosFig(3);
% set(gcf,'Units','normalized'); PosFig = get(gcf,'Position');;
% FigWidth = FigWidthLong; PosFig = PosFigLong*2;
PosFig = get(gca,'Position');
end
if( FigType==4 )
set(gcf,'Units','centimeters')
% PosFig = get(gcf,'Position'); FigWidth = PosFig(3); PosFig = get(gca,'Position');
a=get(gcf,'children');
for i = 1:length(a)
ax = a(i);
if ax.Type == "subplottext" continue; end;
if ax.Type == "legend" continue; end;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
% ax = a(i);
% pos = get(ax, 'Position');
% pos(1) = 0.055;
% pos(3) = 0.9;
% set(ax, 'Position', pos);
end
%no margin
% ax = gca;
% outerpos = ax.OuterPosition;
% ti = ax.TightInset;
% left = outerpos(1) + ti(1);
% bottom = outerpos(2) + ti(2);
% ax_width = outerpos(3) - ti(1) - ti(3);
% ax_height = outerpos(4) - ti(2) - ti(4);
% ax.Position = [left bottom ax_width ax_height];
%todo margin values
% ?latex == tex?
% xlabel('Time (s)','Interpreter','latex','FontSize',14)
% ylabel('Curvature (cm)','Interpreter','latex','FontSize',14)
% legend('$\bar{X}$','$\bar{X} + \sigma$','$\bar{X} - \sigma$','Interpreter','latex','FontSize',14)
saveas(h, strcat(filename, ext));
fprintf(1, ['\t* Zapisano rysunek "%s%s"\n'], filename, ext);
return
end
if( FigType==5 ) % 3 as is on monitor
% set(gcf,'Units','centimeters')
PosFig = get(gcf,'Position'); FigWidth = PosFig(3);
% set(gcf,'Units','normalized'); PosFig = get(gcf,'Position');;
% FigWidth = FigWidthLong; PosFig = PosFigLong*2;
PosFig = get(gca,'Position');
H = figure(gcf);
a = get(H,'Position');
H.WindowState = 'maximized';
figPW(ext);
set(H,'Position',a);
return
end
if( FigType==6 )
set(gca,...
'FontName',font);
set(get(gca,'Xlabel'),...
'FontName',font);
set(get(gca,'Ylabel'),...
'FontName',font);
set(get(gca,'Title'),...
'FontName',font);
set(get(gca,'Legend'),...
'FontName',font);
figP
end
% figure('Units','centimeters',...
%'Position',[FigX0 FigY0 FigWidth FigHeight],...
%'PaperPositionMode','auto');
set(0,'defaultLineLineWidth', LineWidth); % set the default line width to lw
set(0,'defaultLineMarkerSize',MarkerSize); % set the default line marker size to msz
%set(0,'defaultLineStyle',LineStyle); % set default line style
set(0,'DefaultFigurePaperPositionMode','auto'); % automatyczna wielkoć rysunku
%set(0,'DefaultAxesColorOrder',[0,0,0]); % czarna linia
%set(h,'markers',MarkerSize);
%set(0,...
%'LineStyle',LineStyle,...
%'LineWidth',LineWidth,...
%'MarkerSize',MarkerSize);
set(gcf,'DefaultLineLineWidth',LineWidth); % gruboć linii wykresów
%set(gcf,'DefaultMarkerSize',MarkerSize); % wielkoć markera
set(gcf,'Units','centimeters'); % jednostka wymiarowania okna
set(gcf,'Position', [FigX0 FigY0 FigWidth FigHeight]); % wymiary okna: (x,y,dx,dy), (x,y)- lewy dolny
set(gcf,'PaperPositionMode','auto');
% PosFig = get(gcf,'Position');
%set(gca,'Units','centimeters'); %JB
set(gca,... % axis features
'Units','normalized',...
'LineWidth',LineWidth,...
'GridLineStyle',':', ...
'GridAlpha',GridAlpha, ...
'Position',PosFig,... % (left,bottom) (width,hight) - relative, inside the window
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',FontSizeTicks,...
'FontName',font);
set(get(gca,'Xlabel'),...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',FontSizeLabels,...
'Interpreter','tex',...
'FontName',font);
set(get(gca,'Ylabel'),...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',FontSizeLabels,...
'Interpreter','tex',...
'FontName',font);
set(get(gca,'Title'),...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',FontSizeTitle,...
'Interpreter','tex',...
'FontName',font);
set(get(gca,'Legend'),...
'FontUnits','points',...
'FontSize',FontSizeLegend,...
'Interpreter','tex',...
'Location','SouthEast',...
'FontName',font);
if(variantHorizontalLegend)
set(get(gca,'Legend'),...
'Orientation','horizontal',...
'Location','SouthEast');
end
% Legenda
child = get( gcf, 'Children' );
for i = 1:length( child )
tag = get( child(i), 'Tag' );
if( 1 == strcmp( tag, 'legend' ) )
set( child(i),'FontSize',FontSizeLegend,'LineWidth',LineWidth);
break;
end
end
% MyFigFile = fn
% print( strcat(filename,'.png'),'-dpng', '-r600');
% print( strcat(MyFigFile,'.eps'),'-depsc', '-r600');
% end
figP
end
% set(gcf,'Resize','off')
function [name,val] = nextname(bnm,sfx,ext,otp) %#ok<*ISMAT>
% Return the next unused filename, incrementing a numbered suffix if required.
%
% (c) 2017-2021 Stephen Cobeldick
%
%% Examples %%
%
%%% Current directory contains files 'A1.m', 'A2.m', and 'A4.m':
%
% >> nextname('A','1','.m')
% ans = 'A3.m'
%
% >> nextname('A','001','.m')
% ans = 'A003.m'
%
%%% Subdirectory 'HTML' contains folders 'B(1)', 'B(2)', and 'B(4)':
%
% >> nextname('HTML\B','(1)','')
% ans = 'B(3)'
%
% >> nextname('HTML\B','(001)','')
% ans = 'B(003)'
%
% >> nextname('HTML\B','(1)','',false) % default = name only.
% ans = 'B(3)'
% >> nextname('HTML\B','(1)','',true) % prepend same path as the input name.
% ans = 'HTML\B(3)'
%
%% Inputs and Outputs %%
%
%%% Input Arguments (**==default):
% bnm = CharVector or StringScalar giving the base filename or folder name,
% with an absolute or relative file path if required.
% sfx = CharVector or StringScalar of the suffix to append, contains exactly
% one integer number (use leading zeros to specify the number of digits).
% ext = CharVector or StringScalar of the file extension, if any. For folder
% names or files without extensions use ''.
% otp = LogicalScalar, true/false** -> output with same path as input name / name only.
%
%% Input Wrangling %%
%
bnm = nn1s2c(bnm);
sfx = nn1s2c(sfx);
ext = nn1s2c(ext);
%
msg = 'Input <%s> must be a 1xN char vector or a scalar string.';
assert(ischar(bnm)&&ndims(bnm)==2&&size(bnm,1)==1,'SC.nextname:bnm:NotText',msg,'bnm')
assert(ischar(sfx)&&ndims(sfx)==2&&size(sfx,1)==1,'SC:nextname:sfx:NotText',msg,'sfx')
assert(ischar(ext)&&ndims(ext)==2&&size(ext,1)<=1,'SC:nextname:ext:NotText',msg,'ext')
%
tkn = regexp(sfx,'\d+','match');
val = sscanf(sprintf(' %s',tkn{:}),'%lu'); % faster than STR2DOUBLE.
assert(numel(val)==1,'SC:nextname:sfx:NotOneInteger',...
'The suffix must contain exactly one integer value (any number of digits).')
wid = numel(tkn{1});
%
%% Get Existing File / Folder Names %%
%
[inpth,fnm,tmp] = fileparts(bnm);
fnm = [fnm,tmp];
%
% Find files/subfolders on that path:
raw = dir(fullfile(inpth,[fnm,regexprep(sfx,'\d+','*'),ext]));
%
% Special case of exactly one matching subfolder (Octave):
if ~all(strncmp({raw.name},fnm,numel(fnm)))
raw = dir(fullfile(inpth,'*'));
end
%
% Generate regular expression:
rgx = regexprep(regexptranslate('escape',sfx),'\d+','(\\d+)');
rgx = ['^',regexptranslate('escape',fnm),rgx,regexptranslate('escape',ext),'$'];
%
% Extract numbers from names:
tkn = regexpi({raw.name},rgx,'tokens','once');
tkn = [tkn{:}];
%
%% Identify First Unused Name %%
%
if numel(tkn)
% For speed these values must be converted before the WHILE loop:
vec = sscanf(sprintf(' %s',tkn{:}),'%lu'); % faster than STR2DOUBLE.
%
% Find the first unused name, starting from the provided value:
while any(val==vec)
val = val+1;
end
end
%
name = [fnm,regexprep(sfx,'\d+',sprintf('%0*u',wid,val)),ext];
%
if nargin>3
assert(islogical(otp)&&isscalar(otp),'SC:nextname:otp:NotLogicalScalar',...
'Input <otp> must be a scalar logical (i.e. true or false).')
if otp
name = fullfile(inpth,name);
end
end
%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%nextname
function arr = nn1s2c(arr)
% If scalar string then extract the character vector, otherwise data is unchanged.
if isa(arr,'string') && isscalar(arr)
arr = arr{1};
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%nn1s2c
%% make new figure with one subplot of from old fig
% nFig = 6;
% for i=14
% figure(nFig); ncol = i;
% subplot(4,4,ncol);
% ax1=gca;
%
% figure;
% tcl=tiledlayout(1,1);
% ax1.Parent=tcl;
% ax1.Layout.Tile=1;
% % ax1.XLabel = tcl.XLabel
% % ax1.YLabel = tcl.YLabel
%
% figPW(nFig, ncol, 'png', 'figury/', 1, 1, 3)
% end