Main Content

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(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.

example

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

Examples

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; 
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

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

Algorithms

collapse all

Version History

Introduced before R2006a