Access data in cell and save as vector with corresponding variable name

hey there,
i am creating a cell with tables in it when i read a mf4 file. (see picture 1)
Thats the structure of each table (picture 2):
I can access each variable easily with the brackets and save them with the wanted name.
However I want to automate it.
For instance search for d2s and get the values out of the table and save it in a vector in double format with the vector named after the variable.
t does not need to be extracted.
Thanks in advance! HMU if you need more!

4 comentarios

As usual the recommendation is: don't do this! Creating variables dynamically is a shot in your knee. See TUTORIAL: Why and how to avoid Eval
You can create a struct instead, which contains the wanted vectors.
Dennis
Dennis el 27 de Sept. de 2022
Editada: Dennis el 27 de Sept. de 2022
I need the variables with the exact name in the workspace since they are needed for further calculations.
Is there any possibily to realize that?
Did you read the link? You find the "bad solution" there also as deprecated method, because it causes more trouzbles as it solves. But of course, if you really want to apply this, do it.
Could you then help me with creating the struct?

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 29 de Sept. de 2022
Editada: Jan el 29 de Sept. de 2022
Data = struct(); % [EDITED], was: struct([]);
for k = 1:numel(tableCell)
C = tableCell{k};
NameList = T.Properties.VariableNames;
NameList = setdiff(NameList, {'t'}); % Ignore "t"
for iName = 1:numel(NameList)
Name = NameList{iName};
Data.(Name) = C.(Name);
end
end

3 comentarios

Thanks!
I just received an error saying:
A dot name structure assignment is illegal when the structure is empty. Use a subscript on the structure.
in line Data.(Name) = C.(Name);
@Dennis: I've improved the code.
Thank you again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 27 de Sept. de 2022

Comentada:

el 30 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by