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

조회 수: 8 (최근 30일)
K E
K E 2012년 4월 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에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by