MATLAB Answers

Include global variable declaration infunctions

9 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.
Thanks

  5 Comments

Show 2 older comments
Rik
Rik on 21 Nov 2019
Using global variables is almost never needed. They are very sensitive to bugs (e.g. bugs caused by name collisions). Especially for single purpose functions that shouldn't be necessary. It also goes against the concept of modular programming.
What is your use case? We may be able to suggest suitable alternatives.
Adam Danz
Adam Danz on 21 Nov 2019
If the variables that are meant to be global are stored in a script that is loaded at the top of a function, they aren't really "global variables" in terms of their scope (ie, they aren't declared as global). Here's a summary of the potential problems I see in loading variables from a script:
  • Variables that are loaded from a script "pop into existence" which often makes error tracing difficult. For example, a variable defaultSpeed appears in the function and there's really no way for someone to know where it came from other than assuming it came from the script.
  • When making changes to the code, there is the possibility that a local variable is created with the same name as a script-variable which would cause an undetectable override of the 'global' variable.
  • If the script variable values change for whatever reason, variables that are intended to be global may have different values between the various functions that load the script.
  • If the function is called more than once, loading the variables becomes redundant and time consuming. Declaring them persistent and loading them conditionally would solve that, of course. That would also be resilient to the values of those variables changing if the script values change.
  • The function is no longer a modular unit (as Rik mentioned) and becomes dependent on the availability if the script file and the variables assumed to be within the script file.
If the ability to locally debug a function is the only reason against using the nested function approach or the structure-input approach, you could always add a commented line of code to the top of the functions that would load the variables when you need to troubleshoot the function.
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
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
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

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2019b