Can the output of 'whos' operating on a workspace be duplicated for a structure array?

4 次查看(过去 30 天)
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!
  10 个评论
Jason
Jason 2018-3-7
编辑:John Kelly 2018-3-7
@ Jan - Thank you again for your code sample - it pointed me in the direction I needed to go. Your "brown field" analogy is apt - I'm working with legacy code the best I can and working to make sure it doesn't become a deeper quagmire than it already is. Thank you for providing a technically helpful answer despite your misgivings.
Jan
Jan 2018-3-7
编辑:John Kelly 2018-3-7
@Jason: It seems like you are a experienced and responsible programmer, who can handle the dirty chain-saw tool I've provided. But newcomers in the forum might feel encouraged to use the evil eval methods and meta-programming also. Therefore Stephen's warnings and explanations are very important and useful.
@All readers: If you struggle with who, eval and other meta-programming methods, read Stephen's valuable Tutorial: Why and how to avoid eval.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2018-3-6
编辑:Jan 2018-3-6
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).

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by