How Matlab access data in nested structures?

조회 수: 2 (최근 30일)
David Morin
David Morin 2017년 12월 29일
답변: Neil Guertin 2018년 1월 4일
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;

채택된 답변

Neil Guertin
Neil Guertin 2018년 1월 4일
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개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by