-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeep.m
68 lines (55 loc) · 1.83 KB
/
keep.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
function keep(varargin);
%KEEP keeps the caller workspace variables of your choice and clear the rest.
% Its usage is just like "clear" but only for variables.
%
% Xiaoning (David) Yang xyang@lanl.gov 1998
% Revision based on comments from Michael McPartland,
% michael@gaitalf.mgh.harvard.edu, 1999
% Keep all
if isempty(varargin)
return
end
% See what are in caller workspace
wh = evalin('caller','who');
% Check workspace variables
if isempty(wh)
error(' There is nothing to keep!')
end
% Construct a string containing workspace variables delimited by ":"
variable = [];
for i = 1:length(wh)
variable = [variable,':',wh{i}];
end
variable = [variable,':'];
% Extract desired variables from string
flag = 0;
for i = 1:length(varargin)
I = findstr(variable,[':',varargin{i},':']);
if isempty(I)
% disp([' ',varargin{i}, ' does not exist!'])
flag = 1;
elseif I == 1
variable = variable(1+length(varargin{i})+1:length(variable));
elseif I+length(varargin{i})+1 == length(variable)
variable = variable(1:I);
else
variable = [variable(1:I),variable(I+length(varargin{i})+2:length(variable))];
end
end
% No delete if some input variables do not exist
if flag == 1
% disp(' No variables are deleted!')
return
end
% Convert string back to cell and delete the rest
I = findstr(variable,':');
if length(I) ~= 1
for i = 1:length(I)-1
if i ~= length(I)-1
del(i) = {[variable(I(i)+1:I(i+1)-1),' ']};
else
del(i) = {variable(I(i)+1:length(variable)-1)};
end
end
evalin('caller',['clear ',del{:}])
end