This repository has been archived by the owner on Jan 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavex.m
143 lines (118 loc) · 3.93 KB
/
savex.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
function savex(varargin)
% % SAVEX
% %
% % save all variables that are present in the workspace EXCEPT what you
% % specify explicitly (by including the variable names or by using regular
% % expressions).
% %
% % Author : J.H. Spaaks
% % Date : April 2009
% % MATLAB version : R2006a on Windows NT 6.0 32-bit
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
% % test1a = 0;
% % test2a = 2;
% % test3a = 4;
% % test4a = 6;
% % test5a = 8;
% %
% % test1b = 1;
% % test2b = 3;
% % test3b = 5;
% % test4b = 7;
% % test5b = 9;
% %
% % % This example saves all variables that are present in the workspace except
% % % 'test2a' and 'test5b'. 'test3' is ignored since there is no variable by
% % % that name:
% % savex('save-by-varname.mat','test2a','test3','test5b')
% %
% % % This example saves all variables that are present in the workspace except
% % % 'test4a', 'test4b' and 'test2b':
% % savex('save-by-regexp.mat','-regexp','test4[ab]','t[aeiou]st2[b-z]')
% %
% % % This example saves all variables that are present in the workspace except
% % % those formatted as Octave system variables, such as '__nargin__':
% % savex('no-octave-system-vars.mat','-regexp','^__+.+__$')
% %
% % % This example saves all variables that are present in the workspace except
% % % those which are specified using regular expressions, saving in ascii
% % % format. Supported options are the same as for SAVE.
% % savex('save-with-options.txt','-regexp','test4[ab]',...
% % 't[aeiou]st2[b-z]','-ascii')
% %
% %
% %
% % % clear
% % % load('save-by-varname.mat')
% % %
% % % clear
% % % load('save-by-regexp.mat')
% % %
% % % clear
% % % load('no-octave-system-vars.mat')
% % %
% % % clear
% % % load('save-with-options.txt','-ascii')
% % %
varList = evalin('caller','who');
saveVarList = {};
if ismember(nargin,[0,1])
% save all variables
saveVarList = varList
for u = 1:numel(saveVarList)
eval([saveVarList{u},' = evalin(',char(39),'caller',char(39),',',char(39),saveVarList{u},char(39),');'])
end
save('matlab.mat',varList{:})
elseif strcmp(varargin{2},'-regexp')
% save everything except the variables that match the regular expression
optionsList = {};
inputVars ={};
for k=3:numel(varargin)
if strcmp(varargin{k}(1),'-')
optionsList{1,end+1} = varargin{k};
else
inputVars{1,end+1} = varargin{k};
end
end
for k=1:numel(varList)
matchCell = regexp(varList{k},inputVars,'ONCE');
matchBool = repmat(false,size(matchCell));
for m=1:numel(matchCell)
if ~isempty(matchCell{m})
matchBool(m) = true;
end
end
if ~any(matchBool)
saveVarList{end+1} = varList{k};
end
end
for u = 1:numel(saveVarList)
eval([saveVarList{u},' = evalin(',char(39),'caller',char(39),',',char(39),saveVarList{u},char(39),');'])
end
save(varargin{1},saveVarList{:},optionsList{:})
elseif ischar(varargin{1})
% save everything except the variables that the user defined in
% varargin{2:end}
optionsList = {};
inputVars = {};
for k=2:numel(varargin)
if strcmp(varargin{k}(1),'-')
optionsList{1,end+1} = varargin{k};
else
inputVars{1,end+1} = varargin{k};
end
end
for k=1:numel(varList)
if ~ismember(varList{k},inputVars)
saveVarList{end+1} = varList{k};
end
end
for u = 1:numel(saveVarList)
eval([saveVarList{u},' = evalin(',char(39),'caller',char(39),',',char(39),saveVarList{u},char(39),');'])
end
save(varargin{1},saveVarList{:},optionsList{:})
else
error('Unknown function usage.')
end