How Matlab access data in nested structures?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
David Morin
il 29 Dic 2017
Risposto: Neil Guertin
il 4 Gen 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 Commenti
Risposta accettata
Neil Guertin
il 4 Gen 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!