-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsaveMeSomeFigs.m
More file actions
115 lines (101 loc) · 3.62 KB
/
saveMeSomeFigs.m
File metadata and controls
115 lines (101 loc) · 3.62 KB
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
function saveMeSomeFigs(shouldSaveFigures, saveTitle, plotHandles, ...
increaseQuality)
%%% saveMeSomeFigs(shouldSaveFigures, saveTitle, plotHandles, ...
%%% increaseQuality)
%%%
%%% Takes a boolean toggle (shouldSaveFigures) and a string with the
%%% save name (saveTitle), including the path, that you want to save
%%% the current figure as. Example saveTitle looks like the following:
%%%
%%% saveTitle = '../Figures/Velocity vs Time.pdf' - this would save
%%% a figure named 'Velocity vs Time.pdf' (as a .pdf) to a folder
%%% called 'Figures' up one directoy from the code's working
%%% directory.
%%%
%%% Uses the gca function to pull the current figure, normalize
%%% and scale it to the deafult paper size, and save it as a .pdf.
%%%
%%% Examples function call:
%%%
%%% x = linspace(0, 2*pi, 100);
%%% y = sin(x);
%%% plot(x, y)
%%% title('saveMeSomeFigs Test')
%%%
%%% saveTitle = 'saveMeSomeFigs Test Plot.pdf';
%%% shouldSaveFigures = true;
%%% saveMeSomeFigs(shouldSaveFigures, saveTitle)
%%%
%%% Last Modified: 12/7/2017
%%% Date Created: 2/10/2017
%%% Author: Nicholas Renninger
%% Input Sanitization
if ~any(nargin)
error('Gotta use some args there m8.')
elseif nargin < 2
error('Need more than 2 args');
elseif nargin < 3
increaseQuality = false;
plotHandles = NaN;
elseif nargin < 4
if isa(plotHandles, 'cell')
increaseQuality = false;
else
if isa(plotHandles, 'logical')
increaseQuality = plotHandles;
elseif isa(plotHandles, 'matlab.ui.Figure')
increaseQuality = false;
else
error('increaseQuality must be of type: logical')
end
end
end
if isa(plotHandles, 'cell') || isa(saveTitle, 'cell')
if isa(plotHandles, 'cell') && isa(saveTitle, 'cell')
lenHandles = length(plotHandles);
lenTitles = length(saveTitle);
if lenTitles ~= lenHandles
error('plotHandles and saveTitle must be the same length')
end
else
error(['if either plotHandles and saveTitle are cell', ...
' objects, both must be']);
end
else
if isa(saveTitle, 'char')
saveTitle = {saveTitle};
else
error('saveTitle must be of type: char')
end
if isa(plotHandles, 'matlab.ui.Figure')
plotHandles = {plotHandles};
else
plotHandles = {gcf};
end
end
%% Actual function
% setup and save figure as .pdf
if shouldSaveFigures
for i = 1:length(saveTitle)
curr_fig = plotHandles{i};
set(curr_fig, 'PaperOrientation', 'landscape');
set(curr_fig, 'PaperUnits', 'normalized');
set(curr_fig, 'PaperPosition', [0 0 1 1]);
[fid, errmsg] = fopen(saveTitle{i}, 'a');
if fid ~= -1
fclose(fid);
else
while (fid == -1)
error(['Error Opening File in fopen: \n%s.', ...
' It''s probably already open somewhere.'],...
errmsg);
end
end
if increaseQuality
print(curr_fig, '-dpdf', saveTitle{i}, '-opengl', '-r400');
else
print(curr_fig, '-dpdf', saveTitle{i});
end
end
end
end