Clear Filters
Clear Filters

Grab and reference data from OpeningFcn?

1 view (last 30 days)
I was wondering if there's a way to call data from OpeningFcn in a Matlab GUI. I need to reference an initialized value in OpeningFcn to one that's updated with the user input once a pushbutton is pressed.
The initialized data (xi) is compared to the user's gui input _(xgui). If they're not equal, an if statement will run.
if xi ~= xgui
xi = xgui
for ...
...(some other code)...
I've tried creating variables and structures in OpeningFcn, referencing it using handles.OpeningFcn, but I can't seem to figure it out. Can someone please help?

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Jul 2017
rtbme17 - if you have (for example) variables in your OpeningFcn that you want to reference later, then save them to the handles structure. For example,
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% do some stuff
x = 42;
% save x to the handles structure
handles.x = x;
guidata(hObject, handles);
Note that the guidata(hObject, handles); is important as it will save the updated handles structure so that all other callbacks have access to this updated structure (and so can access x). Then in some callback you would do
function pushbutton1_Callback(hObject, eventdata, handles)
% use x
if isfield(handles, 'x')
y = handles.x * 2;
% etc.
In the above, we check to see if x is a fields within handles before trying to use it.

More Answers (0)


Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!