Refer to structure names
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Joanna Przeworska
le 30 Mar 2021
Commenté : Joanna Przeworska
le 1 Avr 2021
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
....
0 commentaires
Réponse acceptée
Stephen23
le 30 Mar 2021
Modifié(e) : Stephen23
le 30 Mar 2021
F = fieldnames(A);
B.(F{1}) = table1
B.(F{2}) = table2
6 commentaires
Stephen23
le 1 Avr 2021
Modifié(e) : Stephen23
le 1 Avr 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.
Plus de réponses (2)
Image Analyst
le 30 Mar 2021
"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.
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!