Loading data from structure, using variable

12 visualizaciones (últimos 30 días)
준영 오
준영 오 el 26 de En. de 2021
Editada: Stephen23 el 26 de En. de 2021
HI
I have loaded a structure format, and in this format, I want to load the y values
What I want to do is to load these structures, not by typing all of them individually, but using for loop
For example,
freq=2048;
DOF=3;
Axis={'x','y','z'};
CAxis={'X','Y','Z'};
SUBA = zeros(freq*2+1,DOF,DOF);
for i=1:DOF
for j=1:DOF
load(strcat('SUB_A5_A1_',string(Axis(i)),'_',string(Axis(j))));
var=strcat('FRF_A_5__',string(CAxis{i}),'_A_1__',string(CAxis{j}));
SUBA(:,i,j)=var.y_values.values(1,:);
end
end
So for each loading structure, their name is like SUB_A5_A1_x_x or SUB_A5_A1_y_y etc...
For each structure, there is one more structure named like FRF_A_5__X_A_1__X or FRF_A_5__Y_A_1__Y etc... (Let's call them 'insidestructure')
I don't want to make all the lines for them, so I first thought using a variable 'var' for naming these 'insidestructure'. However, this didn't work. How can I treat this?
Thanks a lot
  1 comentario
Stephen23
Stephen23 el 26 de En. de 2021
Your description and example are not clear.
Is 'SUB_A5_A1_x_x' the name of the structure (saved in the mat file, which is what your description states), or is it the name of the file (which is what your code indicates)? Or is it both of these (i.e. the files are very badly designed and contain one variable with the same name as the file itself)?

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 26 de En. de 2021
Editada: Stephen23 el 26 de En. de 2021
The most important step is to always load into an output variable (which itself is a scalar structure):
S = load(...) % always load into an output structure!
Then you can easily access the fields of that structure:
In case there is only one variable in the mat file and you do not know its name (i.e the data are very badly designed but out of your control), then you can use this shortcut:
S = load(...); % S is a scalar structure
C = struct2cell(C);
A = C{1} % A is the loaded data array
If the variable saved in the mat file is itself a structure, then A will be that structure. You can use fieldnames to gets a list of its fields, and access the fields as shown here:
I would have fixed your code, but it has too many strange "features" for me to understand how it is supposed to work, e.g. the load line does not change in the loops so always loads the same file, the sprintf seems to be totally superfluous, etc.
In any case, this question is a classic example of why forcing meta-data into variable names or fieldnames is very bad data design, forcing users into writing slow, complex, inefficient code to do very basic tasks (like accessing their data). Much beter data design would save the meta-data in variables, not in variable/field names, which would make processing your data much much much easier. I guess that is a lesson learned the hard way.
  3 comentarios
Stephen23
Stephen23 el 26 de En. de 2021
Editada: Stephen23 el 26 de En. de 2021
"I understand what is the problem, but is there any way handling this?"
Of course. This page (which I also linked to in my answer) explains how:
S.(var).y_values
준영 오
준영 오 el 26 de En. de 2021
Oh my I've read and I thought I tried it... I was just out of my mind..
Sorry for taking your time.
Thanks a lot. Really appreciate it

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

Community Treasure Hunt

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

Start Hunting!

Translated by