Borrar filtros
Borrar filtros

How do access a variable from structure based on a string name of the structure?

2 visualizaciones (últimos 30 días)
I have a simulink model simulating different heat producers in a house. From this simulation I get a lot of data on the demand, the different suppliers, the power consumed etc.
The idea is to be able to run multiple simulations (outputs 1, 2 etc.) and present data for a single simulation, but also be able to compare data. This means making plots with data from multiple simulations, generating some tables with relevant variable. Each simulation has the same variable names, so that is why I need a way to acess data from multiple simulations. It can also be that an input is added and then I still would like to compare it to previous performed simulations with a different amount of variables (that is why the question dissimilar structures).
S1 = load ('Outputs1' )
S2 = load ('Outputs2' )
for i = 1:2
S(i).var1
S(i).var2
end
Is something like possible? I know i can do this: fldnm = 'varnam1'; % S1.(fldnm)
but the option above i have not yet found. I want to do this since I have multiple output files with many the same variables which i want to be able to plot and compare.
  1 comentario
Stephen23
Stephen23 el 6 de Jun. de 2018
This is easy when you use indexing, exactly as KSSV's answer shows. Indexing is what you should use.
This is slow, complex, and buggy if you use lots of separate variable names and try to access the dynamically (which is what you were trying to do). All experienced MATLAB users will advise you to avoid doing this. Read this to know why:

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 6 de Jun. de 2018
Very much possible...check the below demo code:
S1.name = 'Tom' ; S1.age = 14 ;
S2.name = 'Dick' ; S2.age = 15 ;
S(1) = S1 ;
S(2) = S2 ;
for i = 1:2
S(i).name
S(i).age
end
  7 comentarios
Steven Lord
Steven Lord el 6 de Jun. de 2018
Why don't you tell us a little more about how you're planning to use those variables? With that knowledge we may be able to offer guidance on what technique or techniques will allow you to most effectively work with your data.
Mattijs Mientki
Mattijs Mientki el 6 de Jun. de 2018
Sorry that I was not that clear, I update the question description to describe more clearly what I want to be able to do.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by