Pass data from a callback function

8 visualizzazioni (ultimi 30 giorni)
Matthias Whitney
Matthias Whitney il 28 Ott 2016
Risposto: Geoff Hayes il 30 Ott 2016
I have a pretty simple popup window that allows the user to enter a few strings and then push an ‘okay’ button. The callback for the button is supposed to write the input into the handles structure and then close the popup.
However, once the callback function is completed, the new entries to the structure are gone. Is there something I’m missing to pass the data back to the main function?
function popupWindow
%create and populate popup window
handles.popup.f = figure('Name', 'Enter Data', 'Position', [360 502 300 420]);
handles.popup.textInst = uicontrol('Style','text','Units','normalized','String','Please Enter Data','Position',[.12 .88 .8 .05]);
handles.popup.editSerial = uicontrol('Style','edit','Units','normalized','Position',[.1 .7 .8 .05]);
handles.popup.textSerial = uicontrol('Style','text','Units','normalized','String','Serial Number','Position',[.11 .645 .8 .05]);
handles.popup.editName = uicontrol('Style','edit','Units','normalized','Position',[.1 .6 .8 .05]);
handles.popup.textName = uicontrol('Style','text','Units','normalized','String','Operator','Position',[.1 .548 .8 .05 ]);
handles.popup.editDate = uicontrol('Style','edit','Units','normalized','String',datestr(datetime),'Position',[.1 .5 .8 .05]);
handles.popup.textDate = uicontrol('Style','text','Units','normalized','String','Date and Time','Position',[.1 .452 .8 .05]);
handles.popup.pbOkay = uicontrol('Style','pushbutton','Unit','normalized','String','OK','Position',[.43 .2 .15 .07]);
%call back function for pbOkay, executes okayButton function
handles.popup.pbOkay.Callback = {@okayButton,handles};
function okayButton(obj, eventData, handles)
%call back function of okay button
%record input data
handles.report.Serial = handles.popup.editSerial.String;
handles.report.name = handles.popup.editName.String;
handles.report.date = handles.popup.editDate.String;
%close window
close(handles.popup.f);
I should also note that this is part of a larger app created in GUIDE. When I plug in the above code with its callback function using guidata(hObject,handles) I get the same result with the new info not included in the handles structure after the callback function finishes.
Thanks

Risposte (1)

Geoff Hayes
Geoff Hayes il 30 Ott 2016
Matthias - since you are using GUIDE, I think what is missing from your callback is the call to guidata so that the updated handles structure is saved and all other callbacks then receive the latest version of this structure. So your callback function would become
function okayButton(hObject, eventData, handles)
%record input data
handles.report.Serial = handles.popup.editSerial.String;
handles.report.name = handles.popup.editName.String;
handles.report.date = handles.popup.editDate.String;
guidata(hObject,handles); % save the updated structure
%close window
close(handles.popup.f);
As an aside, your above example code creates the okay button callback and passes an additional parameter handles. Please remember that this structure is a copy at the time that the callback is created. And so if handles is updated elsewhere, only the older copy of handles will be passed into the function. For scenarios like this, I would pass the handle to the figure (if using GUIDE) and then do the following
function okayButton(hObject, eventData, hFigure)
handles = guidata(hFigure);
% rest of code
guidata(hFigure,handles);
% etc.
It may be possible to just use hObject and so avoid the need to pass in hFigure as
function okayButton(hObject, eventData)
handles = guidata(hObject);
% etc.
since calling guidata on hObject should mean that the parent figure of hObject be used instead (please read the documentation for guidata for more details).

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by