Grab and reference data from OpeningFcn?

1 次查看(过去 30 天)
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!

采纳的回答

Geoff Hayes
Geoff Hayes 2017-7-17
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by