Documentation

guidata

Store or retrieve UI data

Syntax

guidata(object_handle,data)
data = guidata(object_handle)

Description

guidata(object_handle,data)stores the variabledatawith the object specified byobject_handle. Ifobject_handleis not a figure, then the object's parent figure is used.datacan be any MATLAB®变量,但通常是一个结构,enables you to add new fields as required.

guidatacan manage only one variable at any time. Subsequent calls toguidata(object_handle,data)overwrite the previously storeddata.

GUIDE Uses guidata

GUIDE usesguidatato store and maintain the处理structure. In a GUIDE code file, do not overwrite the处理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处理structure.

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

To change the data managed byguidata:

  1. dat的副本a with the commanddata = guidata(object_handle).

  2. Make the desired changes todata.

  3. Save the changed version ofdatawith the commandguidata(object_handle,data).

guidataprovides application developers with a convenient interface to a figure's application data:

  • You do not need to create and maintain a hard-coded property name for the application data throughout your source code.

  • You can access the data from within a local function callback routine using the component's handle (which is returned bygcbo), without needing to find the figure's handle.

If you are not using GUIDE,guidatais particularly useful in conjunction withguihandles, which creates a structure containing the all of the components in a UI.

Examples

This example callsguidatato save a structure containing a figure's application data from within the initialization section of the application code file. The first section shows how to do this within a programmatic UI. The second section shows how the code differs when you use GUIDE to create a template code file. GUIDE provides a处理structure as an argument to all local function callbacks, so you do not need to callguidatato obtain it. You do, however, need to callguidatato save changes you make to the structure.

Using guidata in a Programmatic UI

Calling theguihandlesfunction creates the structure into which your code places additional data. It contains all handles used by the figure at the time it is called, generating field names based on each object'sTagproperty.

% Create figure figure_handle = figure('Toolbar','none'); % create structure of handles myhandles = guihandles(figure_handle); % Add some additional data as a new field called numberOfErrors myhandles.numberOfErrors = 0; % Save the structure guidata(figure_handle,myhandles)

You can recall the data from within a local callback function, modify it, and then replace the structure in the figure:

function My_Callback() % ... % Get the structure using guidata in the local function myhandles = guidata(gcbo); % Modify the value of your counter myhandles.numberOfErrors = myhandles.numberOfErrors + 1; % Save the change you made to the structure guidata(gcbo,myhandles)

Using guidata in GUIDE

If you use GUIDE, you do not need to callguihandlesto create a structure. You can add your own data to the处理structure. For example, this code adds the field,numberOfErrors, to the structure:

% --- Executes just before simple_gui_tab is made visible. function my_GUIDE_GUI_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 simple_gui_tab (see VARARGIN) % ... % add some additional data as a new field called numberOfErrors handles.numberOfErrors = 0; % Save the change you made to the structure guidata(hObject,handles)
Notice that you use the input argumenthObjectin place ofgcboto refer to the object whose callback is executing.

Suppose you needed to access thenumberOfErrorsfield in a push button callback. Your callback code now looks something like this:

% --- Executes on button press in pushbutton1. function my_GUIDE_GUI_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) % ... % No need to call guidata to obtain a structure; % it is provided by GUIDE via the handles argument handles.numberOfErrors = handles.numberOfErrors + 1; % save the changes to the structure guidata(hObject,handles)

Introduced before R2006a

Was this topic helpful?