Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Global variables in MATLAB GUI?

Date: 2022-08-01 11:26:10

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

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.


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com