In how many functions using the global variable, can a global variable be declared?
36 views (last 30 days)
Show older comments
Hi!:)
I have a question: In my software that I am trying to make in Matlab, I have about two hundred functions and I have done a basic theoretical mistake which requires me taking in the temperature variable in all functions but instead of changing all functions in this way because then I have to change many things in many places in app designer, I want just to declare the variable temperatur to be be global in those functions where it is required. I have already declared temperatur as a global variable in two functions of each type of functions I have so I am wondering whether it can be done by the third , meaning can the variable be used as a global variable by three functions at the same time..?
Thanks
1 Comment
Answers (2)
John D'Errico
on 3 Nov 2025 at 18:12
There is no limit on how many functions can use a global variable. At the same time, I will plead, for your own sanity, to reconsider this path. Passing data around via global variables can make your debugging life pure hell. Did one of those functions mistakenly change the value of that variable? Which one? Where did something silly happen?
3 Comments
Walter Roberson
on 3 Nov 2025 at 19:44
It is not strictly necessary to assign the global variable in at least one function -- but if you never assign it then everywhere it is declared it will have the value [] (empty array).
For example, you might have
global debug_flag
if ~isempty(debug_flag)
fprintf('got to position #17342\n');
end
and it would be perfectly valid for debug_flag not to be set anywhere.
Likewise, it is valid for more than one function to assign to the global variable. Common even:
function set_debug
global debug_flag
debug_flag = 1;
end
function clear_debug
global debug_flag
debug_flag = [];
end
Steven Lord
on 3 Nov 2025 at 18:33
A variable in the global workspace is accessible from any workspace where it is declared as global. You may think this is good, as all you have to do is define it as global in the functions where you use it. But this can also be a very bad thing, as any workspace that declares it global (not just your functions) can see and modify the variable. This can be especially problematic if you used a short variable name (temp or t) as some other function can clobber the global variable's value without realizing it is going to break your function (or even knowing your function exists!)
I strongly recommend you either bite the bullet and change your functions' signatures (and call sites) to pass the temperature in and out of those functions, or (since you mention App Designer) store the temperature as a property of the app and modify the functions to use that property instead of a global variable. This still does have some of the problems around access (anyone with the app handle may be able to access it, depending on its Access, GetAccess, and/or SetAccess property attributes) but it does make it more difficult for someone to "accidentally" break your code (they'd have to "accidentally" get access to the app handle rather than just using the same variable name you used).
0 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!