Specifications
4 Programming GUIs
4-12
else
% Increment the error count, and display it
handles.numberOfErrors = handles.numberOfErrors+1;
set(handles.edit1,'String',...
[handles.errorString,num2str(handles.numberOfErrors)]);
guidata(gcbo,handles);
% store the changes
end
Saving the Handles Structure
You must use guidata to save the handles structure whenever you change
values in that structure. The statement,
guidata(gcbo,handles);
saves the current handles structure for the GUI figure.
Obtaining the Updated Handles Structure
There are cases where you must obtain an updated version of the handles
structure. You can do this with a call to
guidata:
handles = guidata(figure_handle);
where figure_handle is the handle of the GUI figure where the handles
structure is stored.
When Do You Need to Reload the Handles Structure
The handles structure follows the same scoping rules as any MATLAB
variable. If you modify it in a subfunction (e.g., a callback) and return to the
main function, you must reload the structure using
guidata.
Callbacks always load the current version of the
handles structure by passing
guidata(gcbo) as an argument.
If You Are Not Using a Handles Structure
If you are writing your own application M-file and are not generating a handles
structure, you can still use the GUI figure’s application data for storing any
data that you want to pass between subfunctions. This mechanism involves:
•Creating a structure containing the data you want to store.
•Storing the structure in the figure’s application data.