Big struct variable to be shared by multiple functions/procedures. Global variable or not?
이전 댓글 표시
Hi,
so I have a case when I have a potentially very large struct type variable which is shared by many functions and/or procedures. I am fully aware of the general allergy towards global variables in any programming community, but I am wondering if this could be an exception. Should I pass big structs as arguments to functions, or let functions operate on a shared global variable? Or is there a 3rd way
Best,
D
댓글 수: 2
Chunru
2023년 5월 11일
You may consider to use class to share data among functions.
Luca Ferro
2023년 5월 11일
or even save the struct in a .mat file and then load it from within the functions without passing it as an argument
답변 (1개)
but I am wondering if this could be an exception
I am not sure why you think it would be an exception. A big advantage of packing a large amount of data into a struct is to make it easier to pass that data around to many functions. It allows you to avoid having a huge list of input arguments in the functions' calling syntax. Using a global would almost defeat the purpose of containing things in a struct.
In any case, there are other alternatives to a global, if you really don't want to include the struct in your calling syntax. You could, for example, nest your functions and make your struct an externally scoped variable,
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!