Store or retrieve UI data
Use this function only with GUIDE, or with apps created using the
data = guidata(obj)
Create a programmatic UI that stores and retrieves counter data when you click on it.
First, create a program file called
progCounter.m. Within the
Create a figure.
Create a structure with a field value initialized to zero.
Store the data in the figure.
Define a callback function that retrieves the data from the figure, changes the data, and stores the new data in the figure again.
Run the program and click inside the figure. The updated data appears in the Command Window.
f = figure; data.numberOfClicks = 0; guidata(f,data) f.ButtonDownFcn = @My_Callback; function My_Callback(src,event) data = guidata(src); data.numberOfClicks = data.numberOfClicks + 1; guidata(src,data) data end
data = struct with fields: numberOfClicks: 1
Create a button in GUIDE, and store and access data when the button
is pressed. To do this, first add a field to the
handles structure and
guidata to update and manage it. (Make sure to add the data to
handles rather than overwriting it. For more information, see How GUIDE Uses guidata.) Then, configure a
callback that uses
guidata to access and store data when the button
To do this, first, open GUIDE and add a push button to the layout area. Then, click
on the Editor icon to open the program file for editing. Find the
OpeningFcn that was automatically created by GUIDE and add a new
% --- Executes just before countClicks is made visible. function countClicks_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to countClicks (see VARARGIN) % Choose default command line output for countClicks handles.output = hObject; handles.numberOfClicks = 0; % Update handles structure guidata(hObject, handles);
Next, find the push button callback function that GUIDE created. Program it to
access the data stored in
handles, modify it, save the changed data
to the structure, and display the new data in the Command Window. Notice that in GUIDE
hObject, in place of
src, to refer to the
object whose callback is executing.
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.numberOfClicks = handles.numberOfClicks + 1; guidata(hObject,handles) data = guidata(hObject)
>> countClicks data = struct with fields: figure1: [1×1 Figure] pushbutton1: [1×1 UIControl] output: [1×1 Figure] numberOfClicks: 1
obj— Graphics object
Graphics object, such as a
Illustration, or UI object. Use this argument to specify the figure
data. If the specified object is not a figure, then the
parent figure of the object will be used to store
Data to store in the figure, specified as any MATLAB data. Typically,
data is specified as a structure,
which enables you to add new fields as needed. For example, create a data structure with
a field called
Category, store the data from the field in the
structure, and display the stored data in the Command
data.Category = 'Projected Growth'; guidata(gcf,data); data = guidata(gcf)
guidata can manage only one variable per parent figure at any time.
Subsequent calls to
guidata(obj,data) overwrite the previously stored
data. Store additional data by creating a structure with multiple fields.
guidata to store and maintain the structure called
handles. In a GUIDE code file, do not overwrite the
handles structure or your program will no longer work. If you need to
store other data, you can do so by adding new fields to the