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!