How can I dynamically create new structures out of existing ones?

Can someone help me create the updated structure?
Thank you!

3 comentarios

Stephen23
Stephen23 el 9 de Jun. de 2022
Editada: Stephen23 el 9 de Jun. de 2022
"from index 1 to 10"
Putting digits into fieldnames is not indexing. Your approach of forcing meta-data (pseudo-indices) into the fieldnames makes this task more complex than if you had used an actual structure array with actual indices:
"I have created a structure with a for loop "
Your task would be so much easier if you used actual indices (and not forcce pseudo-indices into fieldnames).
Probably something like a simple RESHAPE of a structure array would then do what you need.
Stephen23
Stephen23 el 9 de Jun. de 2022
Editada: Stephen23 el 9 de Jun. de 2022
"I cannot name each structure by hand because there are hundreds of them. "
I didn't mention naming the structures by hand. That would be a waste of time. Why do you mention that?
"What do you mean by actual indices?"
I mean actual indices, i.e. numeric references to specific locations in an array, e.g.:
S(1).fld = ..
S(2).fld = ..
S(3).fld = ..
% ^ Actual indices, neat, simple, very efficient.
You did not use indices, you just have some fieldnames that happen to have digit characters in them. Not the same thing at all (and generally more complex to process, as I suspect to be the case here too).
But as I mentioned at the bottom of my comment, your actual task might even be solved with some simpler operation. But we cannot guess what would be required for your actual goal, because of this:
@Yousra Ganouma: Follow Stephen's idea: Do not hide an index in the name of a field, but create an array instead, e.g.:
Data.EES{1}
Data.EES{2}
...
Data.Space(1).ESS{1}
Then the solution to split the data would be trivial and very efficient.
With hinding an index in the name you have decided for a representation of your data, which impedes the processing massively.

Iniciar sesión para comentar.

 Respuesta aceptada

Mitch Lautigar
Mitch Lautigar el 9 de Jun. de 2022
Editada: Image Analyst el 9 de Jun. de 2022
Convert the structure to a table, combine the tables, then convert back to structure.
Googling "struct2table" and "table2struct" will get you there.

Más respuestas (0)

Categorías

Productos

Versión

R2018b

Etiquetas

Preguntada:

GG
el 9 de Jun. de 2022

Editada:

GG
el 13 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by