How Matlab access data in nested structures?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
David Morin
el 29 de Dic. de 2017
Respondida: Neil Guertin
el 4 de En. de 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 comentarios
Respuesta aceptada
Neil Guertin
el 4 de En. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!