I'm working with MATLAB GUI.
When I'm trying to access the variable which was defined with the push button, it is not defined in the pop up menu. The variables; it should be set 'global', so it is defined in the whole program. And I can use it in any callback.
Do you guys have any idea of how to make the variables 'global'?
varN as global in scope. Ordinarily, each MATLAB® function has its own local variables, which are separate from those of other functions and from those of the base workspace. However, if several functions all declare a particular variable name as global , then they all share a single copy of that variable.
The WHO/WHOS commands can show you just the global variables: who global %# Shows just the variable names whos global %# Shows variable information, like size, class, etc. Show activity on this post. If you type whos at the command line Matlab will list all currently defined variables in your workspace.
All variables in MATLAB are stored in a workspace. When manipulating data at the command line, the variables are stored in the MATLAB base workspace. The contents of this workspace can be displayed by using the whos command. MATLAB functions have their own workspaces, separate from the MATLAB workspace.
Wherever a global variable is going to be accessed in your code (say, different script files, functions etc.), it should be declared as such: global globalVariable;
. Eg.:
function myGUI_OpeningFcn(hObject, eventdata, handles, varargin)
global myGlobalVar;
myGlobalVar = [...]
[...]
end
function btnWriteFile_Callback(hObject, eventdata, handles)
global myGlobalVar;
if myGlobalVar [...]
[...]
end
Notice that in both functions the variable is declared as global in order for them to access it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With