MATLAB: Can the output of ‘whos’ operating on a workspace be duplicated for a structure array

classMATLABstructure arrayswhos

Hi guys –
I wrote a loop using 'whos' to filter out GUI figure elements when saving workspace variables so I'm not saving a copy of the GUI itself (the filter excludes all variables in which the 'class' is ui|graphics and/or gui handles).
What I'd like to do now is to replicate that filtering when operating on a structure array that contains different data types. So, is there a Matlab operation that works on a structure array the same way that 'whos' works on a workspace?
Specific technical answers welcome. Thanks!

Best Answer

UNTESTED CODE written in the forums interface:
function Sout = NoGUIFields(Sin)
% Input: Scalar struct
% Output: Input struct without fields, which contain '^matlab\.(ui|graphics)\.') classes
Field = fieldname(Sin);
Data = struct2cell(Sin);
Sout = struct();
for k = 1:numel(Field)
if isa(Data{k}, 'struct')
Sout.(Field{k}) = NoGUIField(Data{k}); % Recursive call
elseif iscell(Data{k})
Cin = Data{k};
Cout = cell(size(Cin));
for iC = 1:numel(Cin)
if isempty(regexp(class(Cin{k}), '^matlab\.(ui|graphics)\.')
Cout{iC} = Cin{iC};
end
end
Sout.(Field{k}) = Cout;
elseif isempty(regexp(class(Data{k}, '^matlab\.(ui|graphics)\.')
Sout.(Field{k}) = Sin.(Field{k});
end
end
end
This removes all fields, which matches the '^matlab\.(ui|graphics)\.' filter. Substructs are scanned recursively. You can expand this for struct arrays easily on demand. I assume there are more cases to be considered, e.g. user defined handle classes. Therefore my estimation remains: This is a dirty programming style and it will cause more troubles than it solves.
By the way: cellfun('isempty', c) is much faster than cellfun(@isempty, c).