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 Comments

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

Sign in to comment.

Answers (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,

Categories

Products

Release

R2021a

Asked:

on 11 May 2023

Edited:

on 11 May 2023

Community Treasure Hunt

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

Start Hunting!