Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
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 Kommentare
Jason
Jason am 7 Mär. 2018
Bearbeitet: John Kelly am 7 Mär. 2018
@ 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 am 7 Mär. 2018
Bearbeitet: John Kelly am 7 Mär. 2018
@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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 6 Mär. 2018
Bearbeitet: Jan am 6 Mär. 2018
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).
  1 Kommentar
Jason
Jason am 6 Mär. 2018
Thank you Jan, I'll take a look shortly and see what this can do. I appreciate your helpful and detailed response.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by