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

11 次查看(过去 30 天)
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 个评论
K E
K E 2012-4-30
This loop does what I want, but is there a 1-line version?
for iName = 1:length(allVars)
disp(allVars(iName).Name)
end

请先登录,再进行评论。

采纳的回答

Geoff
Geoff 2012-4-30
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 个评论
K E
K E 2012-5-1
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 2012-5-1
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.

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by