How to extract all variable names returned by Simulink.findVars?

8 vues (au cours des 30 derniers jours)
K E
K E le 30 Avr 2012
I want to list all variables in my Simulink model, which I have collected via:
allVars = Simulink.findVars(bdroot)
It is easy to retrieve a single variable's name, for example:
allVars(2).Name
But listing them using allVars.Name puts many spaces between each name, so it's hard to see them together. How do I display all the variable names without spaces using a 1-line command? Or do I have to loop through each? Does Simulink.WorkspaceVar correspond to a more familiar data type like a cell array?
  1 commentaire
K E
K E le 30 Avr 2012
This loop does what I want, but is there a 1-line version?
for iName = 1:length(allVars)
disp(allVars(iName).Name)
end

Connectez-vous pour commenter.

Réponse acceptée

Geoff
Geoff le 30 Avr 2012
Do you mean you have tried:
disp(char(allVars(:).Name));
That would pad each name with spaces due to conversion to a char matrix.
If you want a one-liner, you can do a poor-man's loop:
cellfun(@disp, cellstr(char(allVars(:).Name)));
Or indeed:
arrayfun(@(n) disp(allVars(n).Name), 1:length(allVars));
But I don't really see either of these as an advantage in terms of clarity. The benefit is you can wrap it into a lambda function when you want to do it often:
dispvars = @(v) cellfun(@disp, cellstr(char(v(:).Name)));
dispvars(allVars);
  3 commentaires
K E
K E le 1 Mai 2012
Thanks, Geoff. This worked perfectly:
disp(char(allVars.Name))
It was also useful to learn about cellfun, arrayfun, and anonymous functions from your other solutions. I don't use these, but now I will.
Just wondering: Does Simulink.WorkspaceVar correspond to a more familiar Matlab item, like a cell array? If so, I would be able to make better use of it.
Geoff
Geoff le 1 Mai 2012
Sorry, I can't answer that question - I don't have simulink! =) But if you want to know what it is, do this: class(Simulink.WorkspaceVar)
Glad the first thing worked for you. I thought you wanted them displayed without the trailing spaces, hence the trickier options that do the same thing as your loop example.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink 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!

Translated by