Test existence of global variable within function?

Hi, I want to access the status of a variable every where within different function. I created the global variable:
global debugFlag
debugFlag = true;
I create a function idebugging the goal will be to use them when developping other function as:
function output = myawesomefunction(input)
...
doing stuff
...
if isdebugging
fprintf(stuff)
end
...
end
The function I created is this one:
function existing = isdebugging()
%%Check if debugFlag global variable exist and is true
if exist('debugFlag', 'var')
existing = debugFlag;
else
existing = false;
end
end
But this function always return false.
I checked the answer to this question and tried with
W = evalin('caller','whos');
existing = ismember('debugFlag',[W(:).name])
But this always return true. How can I achieve the function I want? I am sure other people are achieving this for debugging, what is the proper method to have some debugging information?
Thank you.

1 件のコメント

Gabor
Gabor 2024 年 1 月 26 日
Before going into all this nonsense:
ismember('variable_of_interest', who('global'))
credit to:Walter Roberson

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2018 年 9 月 12 日
編集済み: Stephen23 2018 年 9 月 12 日

1 投票

You need to also explicitly declare that global variable inside the functions where you want to use it:
function existing = isdebugging()
global debugFlag % <- your need this!
existing = ~isempty(debugFlag) && debugFlag;
end
Note that there is absolutely no point in testing for the existence of that global variable because, as the global documentation clearly describes, "If the global variable does not exist the first time you issue the global statement, it is initialized to an empty 0x0 matrix." So it will always exist, but it can be empty, true, or false, depending on the state of your code. So those are the variable states that you need to check for in your function.
Personally I would recommend that you avoid using a global variable for this: given that you are happy to have a special function isdebugging, then you could easily use a persistent variable, which neatly encapsulates the functionality into one function and prevent unexpected interference with the global variable:
function otp = isdebugging(inp)
persistent state
state = (~isempty(state) && state) || (nargin>0 && inp);
otp = state;
end
Simply call it to set the state, e.g. isdebugging(true).
"I am sure other people are achieving this for debugging, what is the proper method to have some debugging information?"
Most experienced programmers use the inbuilt debugging tools:

3 件のコメント

Gabor
Gabor 2021 年 2 月 2 日
"Note that there is absolutely no point in testing for the existence of that global variable because, as the global documentation clearly describes"
The problem with this, if the Global Variable accidently not defined before the use in the function, but we "define it empty in the function" than it will delete the original variable after we get back to the original state after running the function. This created unwanted empty variables for me numerous times.
Basically it is almost dangerouse to use global variable just because the fact it will start deleting variables if it is not declared where it suppose to. For me it just do not make any sense to defined it empty if it is not defined, thats why I would like to know if it is defined or not so I dont create accedental empty global variables.
Or I just miss the logic... why do we have to define it twice?
"If the global variable does not exist the first time you issue the global statement, it is initialized to an empty 0x0 matrix"
Gabor
Gabor 2021 年 2 月 2 日
I guess my workaround will be that I create it anyway and check it if it empty and if it is than I delete it right away.
Stephen23
Stephen23 2024 年 1 月 26 日
編集済み: Stephen23 2024 年 1 月 26 日
"Basically it is almost dangerouse to use global variable just because the fact it will start deleting variables if it is not declared where it suppose to."
It is certainly "dangerous" to use global variables. They are best avoided.
"Or I just miss the logic... why do we have to define it twice?"
You don't need to define global variables twice. However if you keep redefining it (including via the default value of a GLOBAL call if the variable does not exist in the global workspace) then it will have whatever value you gave it last. That the the whole point of global variables.
Of course you do need to declare it in every workspace where you want to refer to it:
funOne()
funTwo()
3.1416
funThree()
Warning: The value of local variables may have been changed to match the globals. Future versions of MATLAB will require that you declare a variable to be global before you use that variable.
funFour()
3.1416
function funOne()
global someval % this redefines the variable!
someval = pi;
end
function funTwo()
global someval
disp(someval)
end
function funThree()
someval = sqrt(2);
global someval
end
function funFour()
global someval
disp(someval)
end
The warning is there for a reason.
I agree that TMW should forbid usage before the variable is declared. That would resolve your accidental creating of empty variables (as would defining them as global at the top of the code).

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 2 月 3 日

2 投票

ismember('variable_of_interest', who('global'))

4 件のコメント

Gabor
Gabor 2024 年 1 月 26 日
This should be on the top as the one and only normal answer to the question!
Thank you
Stephen23
Stephen23 2024 年 1 月 26 日
編集済み: Stephen23 2024 年 1 月 26 日
"This should be on the top as the one and only normal answer to the question!"
Lets try it right now and see how it fails:
global debugFlag
debugFlag = false; % debug state is FALSE!
YourApproachFails()
If this displays then your approach does NOT work
MyAnswerWorks()
Oh, my answer works. Debugging is disabled!
function YourApproachFails()
if ismember('debugFlag',who('global'))
disp('If this displays then your approach does NOT work')
else
disp('Hurrah! Debugging is disabled!')
end
end
function MyAnswerWorks()
global debugFlag % <- you need this!
if ~isempty(debugFlag) && debugFlag
disp('If this displays then my approach does NOT work')
else
disp('Oh, my answer works. Debugging is disabled!')
end
end
Have a think about how you would need to modify your approach to make it work correctly.
Walter Roberson
Walter Roberson 2024 年 1 月 26 日
ismember('variable_of_interest', who('global'))
detects whether the global variable exists at all, and does not have the side effect of creating the global variable
Stephen23
Stephen23 2024 年 1 月 26 日
"detects whether the global variable exists at all, and does not have the side effect of creating the global variable"
Yes, I am quite aware of that.
The OP makes it clear that they also want to check the value of that global variable. Which this code does not do.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

リリース

R2018a

質問済み:

2018 年 9 月 12 日

コメント済み:

2024 年 1 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by