Big struct variable to be shared by multiple functions/procedures. Global variable or not?
Mostrar comentarios más antiguos
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 comentarios
Chunru
el 11 de Mayo de 2023
You may consider to use class to share data among functions.
Luca Ferro
el 11 de Mayo de 2023
or even save the struct in a .mat file and then load it from within the functions without passing it as an argument
Respuestas (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,
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!