Can the output of 'whos' operating on a workspace be duplicated for a structure array?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Jan
le 7 Mar 2018
Modifié(e) : John Kelly
le 7 Mar 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.
Réponse acceptée
Jan
le 6 Mar 2018
Modifié(e) : Jan
le 6 Mar 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).
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Whos dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!