MATLAB Answers

Include global variable declaration infunctions

7 views (last 30 days)
James Andrada
James Andrada on 21 Nov 2019
Answered: James Andrada on 22 Nov 2019
I'm developing an application that's structured as a set of many small simple single purpose functions
I want all functions to have access to the same set of global variables. If I save the set of "global" statements(and perhaps other variable initializations) in a script file, can I include the script file in each function so no matter how many global variables I need, all functions, by virtue of including the script file, will have access to the total set of global variables and flow control indicators?
A LONG time ago I was a designer and developer of several computer languages including some of IBM's Fortran compilers, PL/1, ec (ancient history I know) and the ability to generate code as part of the compilation process itself was extremely helpful in allowing us to customize the programming language for specific appllcation development cases. This of course would be a very simlpe case of the same concept, I think.
Stephen Cobeldick
Stephen Cobeldick on 21 Nov 2019
"I like the idea of nested functions but during debugging I want to debug each function independently in the workspace"
There is no such thing as "the workspace", just the base workspace and function workspaces:
"I like the idea of nested functions but during debugging I want to debug each function independently in the workspace"
That is what the debugging tools are for. Using the debugging tools is ultimately a much more robust solution than using anti-pattern global variables.

Sign in to comment.

Answers (2)

Jan on 21 Nov 2019
Global variables are a shot in your knee in every case. Hiding them in scripts, which are called dynamically, impedes the debugging even further. Collisions with other software packages, which rely on globals also, can be expected.
If you have a really good reason to use globals (I haven't seen any in the last 30 years), use one struct with a really unique name. Store the set of variables as fields of this struct.
But then it is a tiny step to provide a copy of this struct to all functions also. If a function is allowed to modify the contents, reply the struct in the outputs.
  1 Comment
Rik on 21 Nov 2019
I'm not convinced that it was the best possible solution, but I did use a global variable in a function as a fallback method for very old releases of Matlab (which don't include the addprop function). link. Maybe it should have been stored with setpref and loaded to a persistent variable, but on the other hand that function isn't my best work anyway.

Sign in to comment.

James Andrada
James Andrada on 22 Nov 2019
In the "real" world the application won't run in Matlab. I'm just using Matlab to get the overall logic worked out in a static environment, ie using captured csv files instead of a real time stream of data off of the sensor array. I might have several asynchronous processes all looking at the same stream of data in parallel and interrogating a "parameter array" on every iteration and posting alerts that may influence how some of the other processes react to incoming data.
But all in all I think using a structure that every process looks at on every iteration will do the trick as well as global variables
Thanks to everyone - obviously I'm not a Matlab expert




Community Treasure Hunt

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

Start Hunting!