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

5 views (last 30 days)
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 Comment
K E
K E on 30 Apr 2012
This loop does what I want, but is there a 1-line version?
for iName = 1:length(allVars)
disp(allVars(iName).Name)
end

Sign in to comment.

Accepted Answer

Geoff
Geoff on 30 Apr 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 Comments
K E
K E on 1 May 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 on 1 May 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!