MATLAB: Do I receive a run-time error when using the CLEAR function on a global variable in a stand-alone application

aloneclearcompilecompilerexeexecutableglobalglobalsMATLABrun timestandstand-alonestandalonesubfunctionvariable

I have discovered inconsistent behavior when I CLEAR a global variable within a function MATLAB file (e.g. foo.m) and within its compiled stand-alone executable (foo.exe).
This is my function MATLAB file called foo.m:
function foo
global a % declare global variable "a"

a = 1 % initialize "a"
subfoo % call my subfunction
function subfoo
global a % declare global variable "a"
a % get output of "a"
clear a % clear global variable "a"
global a % declare another global variable "a"
a % get the output of "a"
Here is the result of running my function MATLAB file within MATLAB:
a =
1
a =
1
a =
1
However, after I compile my MATLAB file foo.m into a stand-alone executable foo.exe, the result is the following output:
a =
1
a =
1
Undefined function or variable 'a'.
EXITING .
Why does the CLEAR statement on the global variable "a", actually clear the global variable in a stand-alone executable and cause the above error?

Best Answer

This behavior occurs because of the way that GLOBAL variables function external to MATLAB.
The following describes essentially what is happening with the GLOBAL variable in the MATLAB environment and in a stand-alone application:
Ordinarily, each MATLAB function, defined by an MATLAB file (e.g. foo.m), has its own local variables, which are separate from those of other functions, and from those of the base MATLAB workspace. However, if several functions, and the base workspace, all declare a particular variable name as GLOBAL, they all share a single copy of that variable. Any assignment to that variable, in any function, is available to all the functions declaring it GLOBAL.
Therefore, when using the "clear a" within the foo.m MATLAB File, the variable "a" was deleted from the function workspace, but it still existed in the base MATLAB workspace and hence the value assigned earlier was still persistent (or existed). To properly clear the GLOBAL variable so that it does not exist in either the function workspace or the base workspace, the following MATLAB command must be used:
clear global a
Note, however, if the GLOBAL variable does not exist the first time you issue the GLOBAL statement (such as the last set of statements in the subfunction (subfoo)), it is initialized to an empty matrix, which is expected behavior in the MATLAB environment.
On the other hand, when compiling and running the foo.m MATLAB file in a stand-alone executable, it no longer has a connection to the base MATLAB workspace. Therefore, when using the "clear a" command inside the subfunction, it removed the variable from its application workspace, which is the only existing workspace. The thing to note here, is that when reinitializing the GLOBAL variable "a" and then trying to obtain the value of it again, this variable is no longer present and since there is no base workspace, it cannot initialize "a" to an empty matrix. This is also expected behavior within a stand-alone application.