Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables in MATLAB GUI?

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'?

like image 630
Alvi Syahrin Avatar asked May 11 '13 13:05

Alvi Syahrin


People also ask

Are there global variables in MATLAB?

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.

How do you check global variables in MATLAB?

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.

Where are global variables stored MATLAB?

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.


1 Answers

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.

like image 99
rascob Avatar answered Sep 21 '22 16:09

rascob