How to delete workspace empty variables
Show older comments
Hello, I have a script that creates tons of variables in my workspace. However, some of them are worthless. For example, one will be a 3x1 cell array that is [ [] [] [] ]. How do I delete all these variables that are filled with empty cells?
1 Comment
xander fong
on 21 Jul 2015
Answers (1)
bio lim
on 21 Jul 2015
2 Comments
xander fong
on 21 Jul 2015
bio lim
on 21 Jul 2015
This should work assuming your variables are cell arrays.
c = cell(1,3); % Cell c and e are empty
d = cell(1,2);
e = cell(1,5);
d{1,1} = 4;
d{1,2} = 7;
var = who;
for var_num = 1 : length(var)
if iscell(eval([var{var_num}]))
e = cellfun(@isempty, eval([var{var_num}]));
h = true(size(eval([var{var_num}])));
if isequal(h,e)
eval(['clear ' var{var_num} ';'])
end
end
end
clear var var_num e h
% Remaining variable is d
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!