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

You may consider to use class to share data among functions.
or even save the struct in a .mat file and then load it from within the functions without passing it as an argument

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2023년 5월 11일
편집: Matt J 2023년 5월 11일
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에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2023년 5월 11일

편집:

2023년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by