How Matlab access data in nested structures?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
David Morin
le 29 Déc 2017
Réponse apportée : Neil Guertin
le 4 Jan 2018
Hello all,
I use a nested structure in my program to store and organize variables:
struct.store1.var1 = var1;
struct.store1.var2 = var2;
struct.store1.var3 = var3;
struct.store1.var4 = var4;
struct.store2.var1 = var5;
struct.store2.var2 = var6;
And this going on for around 100 variables (mostly doubles and matrices). My question is should I reorganize struct in an unested structure (like herunder) to improve performances?
struct.store1Var1 = var1;
struct.store1Var2 = var2;
struct.store1Var3 = var3;
struct.store1Var4 = var4;
struct.store2Var1 = var5;
struct.store2Var2 = var6;
0 commentaires
Réponse acceptée
Neil Guertin
le 4 Jan 2018
For a situation like this, code readability and adaptability is much more important than performance. Even if one of these methods is slightly faster than the other, the compute time saved is not worth the extra time you will spend trying to make changes when the requirements for your project change.
If you have hundreds of variables, you also might want to think about completely restructuring your program. Use functions so that each variable is only defined and used where it needs to be, or use a different data structure that better matches your data.
Once the program is done, and if performance is still an issue, you can use the profiler to identify the bottlenecks. I guarantee it won't be the structures.
0 commentaires
Plus de réponses (0)
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!