9

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

Patashu
  • 21,443
  • 3
  • 45
  • 53
Alvi Syahrin
  • 373
  • 3
  • 7
  • 16
  • could you explain better what are you trying to do? i mean, are you trying to perform an action pushing the pushbutton and that action depends on the value of the popup menu? second, are you using GUIDE to program the GUI or are you doing it programmatically? – Eugenio May 11 '13 at 14:22

2 Answers2

8

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.

rascob
  • 453
  • 1
  • 3
  • 11
0

The official way to do this is using the guidata function. http://www.mathworks.com/matlabcentral/answers/88518-create-a-global-variable-in-a-gui

Sonicsmooth
  • 2,673
  • 2
  • 22
  • 35