Main Content


Store or retrieve UI data




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(obj,data) stores the specified data in the application data of 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.


data = guidata(obj) returns previously stored data, or an empty matrix if nothing is stored.


collapse all

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; 
f.ButtonDownFcn = @My_Callback;

function My_Callback(src,event)
data = guidata(src);
data.numberOfClicks = data.numberOfClicks + 1;
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 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;
data = guidata(hObject)
>> countClicks

data = 

  struct with fields:

           figure1: [1×1 Figure]
       pushbutton1: [1×1 UIControl]
            output: [1×1 Figure]
    numberOfClicks: 1

Input Arguments

collapse all

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 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';
data = guidata(gcf)


collapse all

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