Grab and reference data from OpeningFcn?

1 Ansicht (letzte 30 Tage)
rbme17
rbme17 am 17 Jul. 2017
Kommentiert: rbme17 am 17 Jul. 2017
Hi,
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)...
end
end
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?
Thanks!

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 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.
end
In the above, we check to see if x is a fields within handles before trying to use it.

Weitere Antworten (0)

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by