Hi,
Suppose I have a structure A with a, b, c, d tables inside. My aim is to use names of tables within structure A (a, b, c, d) to create new tables in structure B. I tied to do this in the following way, however it does not work. Any ideas how to corect it?
B.(fieldnames(A)'){1} = table1
B.(fieldnames(A)'){2} = table2
....

 Respuesta aceptada

Stephen23
Stephen23 el 30 de Mzo. de 2021
Editada: Stephen23 el 30 de Mzo. de 2021

1 voto

6 comentarios

Joanna Przeworska
Joanna Przeworska el 31 de Mzo. de 2021
Editada: Joanna Przeworska el 31 de Mzo. de 2021
Yes, I used this idea before and it worked, however, was wondering how to omit the first line, and link this part of code with the subsequent lines. Is it possible?
Stephen23
Stephen23 el 31 de Mzo. de 2021
Editada: Stephen23 el 31 de Mzo. de 2021
"Is it possible?"
Yes, but I would not recommend it:
A = struct('abc',123,'xyz',456)
A = struct with fields:
abc: 123 xyz: 456
X = 2; % 2nd fieldname
B.(subsref(fieldnames(A),substruct('{}',{X}))) = 789
B = struct with fields:
xyz: 789
Joanna Przeworska
Joanna Przeworska el 1 de Abr. de 2021
Could you explain why it is not recommended?
Walter Roberson
Walter Roberson el 1 de Abr. de 2021
... because it is hard to read?
Stephen23
Stephen23 el 1 de Abr. de 2021
Editada: Stephen23 el 1 de Abr. de 2021
"Could you explain why it is not recommended?"
Because it is more complex. More complex code is more difficult to understand, more difficult to maintain, obfuscates the code intent, is more liable to bugs, and is more difficult to debug. It is also likely to be less efficient, as it requires multiple calls to fieldnames.
Joanna Przeworska
Joanna Przeworska el 1 de Abr. de 2021
I see your point. Thank you for your help and clarifications.
Best regards, JP

Iniciar sesión para comentar.

Más respuestas (2)

Matt J
Matt J el 30 de Mzo. de 2021

1 voto

args = [fieldnames(A).'; {table1, table2, table3,...}]
B=struct(args{:})

1 comentario

Joanna Przeworska
Joanna Przeworska el 31 de Mzo. de 2021
Dear Matt,
Thank you for your response, whowever this is not exactly what I wanted to receive.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 30 de Mzo. de 2021

0 votos

"Suppose I have a structure A with a, b, c, d tables inside. My aim is to use names of tables within structure A (a, b, c, d) to create new tables in structure B."
So, why does this not work?
B = A;
New structure B will have all the same fields (a, b, c, and d) as existing structure A with the same names. Each field in the structure will have the same table as in A, which is what it seemed like you were trying to do when you assigned the tables. This also means that all the columns names of the tables of structure B will match the column names from the tables of structure A.

1 comentario

Joanna Przeworska
Joanna Przeworska el 31 de Mzo. de 2021
In my exercise, my goal is to dynamically refer to the field names of structure A in order to store the calculation in a different structure B, but with a name taken from structure A.
I don't know if I made it clear what I mean.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Mzo. de 2021

Comentada:

el 1 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by