guidata
Store or retrieve UI data
Description
Note
Storing app data in the UserData
property of the main app figure
is recommended over using guidata
, because it can result in more
readable code. For more information about storing and sharing app data, see Share Data Among Callbacks.
guidata(
stores the specified data in the application data of obj
,data
)obj
if it is a
figure, or the parent figure of obj
if it is another component. For
more information, see How guidata Manages Data.
Examples
Store Data in Programmatic UI
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
program file:
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
Using guidata
in GUIDE
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
use 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
is pressed.
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
field to handles
called numberOfClicks
.
% --- 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
you use 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
Input Arguments
obj
— Graphics object
graphics object
Graphics object, such as a Figure
, Axes
,
Illustration
, or UI object. Use this argument to specify the figure
that stores data
. If the specified object is not a figure, then the
parent figure of the object will be used to store data
.
data
— Data
any MATLAB® data
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
Window:
data.Category = 'Projected Growth';
guidata(gcf,data);
data = guidata(gcf)
Algorithms
How guidata
Manages Data
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.
How GUIDE Uses guidata
GUIDE uses 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 handles
structure.
Version History
Introduced before R2006a
See Also
guide
| guihandles
| getappdata
| setappdata
| struct
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)