Borrar filtros
Borrar filtros

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

11 visualizaciones (últimos 30 días)
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 comentario
K E
K E el 30 de Abr. de 2012
This loop does what I want, but is there a 1-line version?
for iName = 1:length(allVars)
disp(allVars(iName).Name)
end

Iniciar sesión para comentar.

Respuesta aceptada

Geoff
Geoff el 30 de Abr. de 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 comentarios
K E
K E el 1 de Mayo de 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 el 1 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by