Documentation

Share Data Among Callbacks

Overview of Data Sharing Techniques

Many UIs contain interdependent controls, menus, and graphics objects. Since each callback function has its own scope, you must explicitly share data with those parts of your UI that need to access it. The table below describes several different methods for sharing data within your UI.

MethodDescriptionRequirements and Trade-Offs
Store Data in UserData or Other Object Properties

Query or store property values directly though the component object.

All UI components have a UserData property that can store any MATLAB® data.

  • Requires access to the component to set or retrieve the properties.

  • UserData holds only one variable at a time, but you can store multiple values as a struct array or cell array.

Store Data as Application Data

Associate data with a specific component using the setappdata function. You can access it later using the getappdata function.

  • Requires access to the component to set or retrieve the application data.

  • Can share multiple variables.

Create Nested Callback Functions (Programmatic UIs)

Nest your callback functions inside your main function. This gives your callback functions access to all the variables in the main function.

  • Requires callback functions to be coded in the same file as the main function.

  • Not recommended for GUIDE UIs.

  • Can share multiple variables.

Store Data Using the guidata Function

Share data with the figure window using the guidata function.

  • Stores or retrieves the data through any UI component.

  • Stores only one variable at a time, but you can store multiple values as a struct array or cell array.

Store Data in UserData or Other Object Properties

UI components contain useful information in their properties, which you can access directly as long as you have access to the component. Use dot notation, component.property, to store or retrieve a component property. Dot notation works in R2014b and later releases. If you are using an earlier release, use the get and set functions instead.

Use the findobj function to search for a component wherever you code does not have direct access to that component. You can store any data in the UserData property of a UI component or figure. For example, the following programmatic UI code uses the UserData property to share information about the slider. To see how it works, copy and paste this code into an editor and run it.

function my_slider()
hfig = figure();
slider = uicontrol('Parent', hfig,'Style','slider',...
         'Units','normalized',...
         'Position',[0.3 0.5 0.4 0.1],...
         'Tag','slider1',...
         'UserData',struct('val',0,'diffMax',1),...
         'Callback',@slider_callback);
     
button = uicontrol('Parent', hfig,'Style','pushbutton',...
         'Units','normalized',...
         'Position',[0.4 0.3 0.2 0.1],...
         'String','Display Difference',...
         'Callback',@button_callback);
end

function slider_callback(hObject,eventdata)
	sval = hObject.Value;
	diffMax = hObject.Max - sval;
	data = struct('val',sval,'diffMax',diffMax);
	hObject.UserData = data;
	% For R2014a and earlier: 
	% sval = get(hObject,'Value');  
	% maxval = get(hObject,'Max');  
	% diffMax = maxval - sval;      
	% data = struct('val',sval,'diffMax',diffMax);   
	% set(hObject,'UserData',data);   

end

function button_callback(hObject,eventdata)
	h = findobj('Tag','slider1');
	data = h.UserData;
	% For R2014a and earlier: 
	% data = get(h,'UserData'); 
	display([data.val data.diffMax]);
end
When the user moves the slider, the slider_callback function stores the current slider value and the difference between the current and maximum values in the UserData property. When the user clicks the push button, the button_callback function finds the component containing the Tag property, 'slider1'. Then, the last two commands in the button_callback function get and display the values stored in the slider's UserData property, respectively.

You can store any MATLAB variable in the UserData property. The syntax for storing and retrieving data is the same for GUIDE and programmatic UIs.

Store Data as Application Data

You can share data using application data. To store application data, call the setappdata function:

setappdata(obj,name,value);
The first input, obj, is the graphics object in which to store the data. The second input, name, is a string that identifies the value. The third input, value, is the value you want to store.

To retrieve application data, use the getappdata function:

data = getappdata(obj,name);
The component, obj, must be the graphics object containing the data. The second input, name, must match the string you used to store the data. Unlike UserData, which only holds only one variable, you can use setappdata to store multiple variables.

The following programmatic UI code uses application data to share two values. To see how it works, copy and paste this code into an editor and run it.

function my_slider()
hfig = figure();
setappdata(hfig,'slidervalue',0);
setappdata(hfig,'difference',1);

slider = uicontrol('Parent', hfig,'Style','slider',...
         'Units','normalized',...
         'Position',[0.3 0.5 0.4 0.1],...
         'Tag','slider1',...
         'Callback',@slider_callback);
     
button = uicontrol('Parent', hfig,'Style','pushbutton',...
         'Units','normalized',...
         'Position',[0.4 0.3 0.2 0.1],...
         'String','Display Values',...
         'Callback',@button_callback);
end

function slider_callback(hObject,eventdata)
	diffMax = hObject.Max - hObject.Value;
	setappdata(hObject.Parent,'slidervalue',hObject.Value);
	setappdata(hObject.Parent,'difference',diffMax);
	% For R2014a and earlier: 
	% maxval = get(hObject,'Max');  
	% currval = get(hObject,'Value');  
	% diffMax = maxval - currval;   
	% parentfig = get(hObject,'Parent');  
	% setappdata(parentfig,'slidervalue',currval); 
	% setappdata(parentfig,'difference',diffMax); 
end

function button_callback(hObject,eventdata)
	currentval = getappdata(hObject.Parent,'slidervalue');
	diffval = getappdata(hObject.Parent,'difference');
	% For R2014a and earlier:
	% parentfig = get(hObject,'Parent');
	% currentval = getappdata(parentfig,'slidervalue');
	% diffval = getappdata(parentfig,'difference');

	display([currentval diffval]);
end
When the user moves the slider, the slider_callback function calls setappdata to save the new slider value using the name, 'slidervalue'. The next line of code calls setappdata to save another value, diffMax, using the name, 'difference'. Both calls to setappdata store the data in the figure.

When the user clicks the push button, the button_callback function calls getappdata to retrieve the values named 'slidervalue' and 'difference'. Then, the final command in the button_callback function displays those values.

If you are developing a UI using GUIDE, you can use setappdata and getappdata in the same way.

Create Nested Callback Functions (Programmatic UIs)

You can nest callback functions inside the main function of a programmatic UI. When you do this, the nested callback functions share a workspace with the main function. As a result, the nested functions have access to all the UI components and variables defined in the main function. The following example code uses nested functions to share data about the slider position. To see how it works, copy and paste this code into an editor and run it.

function my_slider()
	hfig = figure();
	data = struct('val',0,'diffMax',1);
	slider = uicontrol('Parent', hfig,'Style','slider',...
         'Units','normalized',...
         'Position',[0.3 0.5 0.4 0.1],...
         'Tag','slider1',...
         'Callback',@slider_callback);
     
	button = uicontrol('Parent', hfig,'Style','pushbutton',...
         'Units','normalized',...
         'Position',[0.4 0.3 0.2 0.1],...
         'String','Display Difference',...
         'Callback',@button_callback);

	function slider_callback(hObject,eventdata)
		sval = hObject.Value;
		diffMax = hObject.Max - sval;
		% For R2014a and earlier:
		% sval = get(hObject,'Value');
		% maxval = get(hObject,'Max');
		% diffMax = maxval - sval;

		data.val = sval;
		data.diffMax = diffMax;
	end

	function button_callback(hObject,eventdata)
		display([data.val data.diffMax]);
	end
end
The main function defines a struct array called data. When the user moves the slider, the slider_callback function updates the val and diffMax fields of the data structure. When the end user clicks the push button, the button_callback function displays the values stored in data.

    Note:   Nested functions are not recommended for GUIDE UIs.

Store Data Using the guidata Function

The guidata function provides a way to share data with the figure window. You can store or retrieve the your data in any callback through the hObject component. This means that, unlike working with UserData or application data, you do not have to search for a specific UI component to access your data. Call guidata with two input arguments to store data:

guidata(object_handle,data);
The first input, object_handle, is any UI component (typically hObject). The second input, data, is the variable to store. Every time you call guidata using two input arguments, MATLAB overwrites any previously stored data. This means you can only store one variable at a time. If you want to share multiple values, then store the data as a struct array or cell array.

To retrieve data, call guidata using one input argument and one output argument:

data = guidata(object_handle);
The component you specify to store the data does not need to be the same component that you use to retrieve it.

If your data is stored as a struct array or cell array, and you want to update one element without changing the other elements, then retrieve the data and replace it with the modified array:

data = guidata(hObject);
data.myvalue = 2;
guidata(hObject,data);

Use guidata in a Programmatic UI

To use guidata in a programmatic UI, store the data with some initial values in the main function. Then you can retrieve and modify the data in any callback function.

The following code is a simple example of a programmatic UI that uses guidata to share a struct array containing two fields. To see how it works, copy and paste this code into an editor and run it.

function my_slider()
hfig = figure();
guidata(hfig,struct('val',0,'diffMax',1));
slider = uicontrol('Parent', hfig,'Style','slider',...
         'Units','normalized',...
         'Position',[0.3 0.5 0.4 0.1],...
         'Tag','slider1',...
         'Callback',@slider_callback);
     
button = uicontrol('Parent', hfig,'Style','pushbutton',...
         'Units','normalized',...
         'Position',[0.4 0.3 0.2 0.1],...
         'String','Display Values',...
         'Callback',@button_callback);
end

function slider_callback(hObject,eventdata)
	data = guidata(hObject);
	data.val = hObject.Value;
	data.diffMax = hObject.Max - data.val;
	% For R2014a and earlier:
	% data.val = get(hObject,'Value');
	% maxval = get(hObject,'Max');
	% data.diffMax = maxval - data.val;

	guidata(hObject,data);
end

function button_callback(hObject,eventdata)
	data = guidata(hObject);
	display([data.val data.diffMax]);
end
When the user moves the slider, the slider_callback function calls guidata to retrieve a copy of the stored struct array. The next two lines of code modify the struct array. The last line in the function replaces the stored struct array with the modified copy.

When the end users clicks the push button, the button_callback function calls guidata to retrieve a copy of the stored struct array. Then it displays the two values stored in the array.

Use guidata in a GUIDE UI

GUIDE uses guidata to store a struct array called handles, which contains all the UI components. MATLAB passes the handles array to every callback function. If you want to use guidata to share additional data, then add fields to the handles structure in the OpeningFcn callback. To modify your data in a callback function, modify the handles array, and then store it using guidata. This slider callback function shows how to modify and store the handles array in a callback function.

function slider1_Callback(hObject,eventdata,handles)
	handles.myvalue = 2;
	guidata(hObject,handles);
end 

To see a full example of a GUIDE UI that uses the guidata function, follow these steps to copy and examine the code.

  1. Set your current folder to one to which you have write access.

  2. Copy the example code.

    copyfile(fullfile(docroot, 'techdoc','creating_guis',...
      'examples','sliderbox_guidata.*')),...
       fileattrib('sliderbox_guidata.*', '+w');
    
  3. Display this example in the GUIDE Layout Editor:

    guide sliderbox_guidata.fig
  4. View the code in the Editor by clicking the Editor button, .

  5. Run the program by clicking the Run Figure button, .

Sharing Data Among Multiple GUIDE UIs

Example of Sharing Data Between Two UIs

To see a full-featured example that uses application data and the guidata function to share data between two separate UIs, use these steps to copy, run, and examine the code.

  1. Set your current folder to one to which you have write access.

  2. Copy the example code with this command:

    copyfile(fullfile(docroot, 'techdoc','creating_guis','examples'...
      ,'changeme*.*')),fileattrib('changeme*.*', '+w');
    
  3. Display the UIs in two GUIDE Layout Editors with these commands:

    guide changeme_main
    guide changeme_dialog

  4. View the code in the editor by clicking the Editor button, , in both Layout Editors.

  5. Run the changeme_main program by clicking Run Figure button, , in that UI's Layout Editor.

Example of Sharing Data Among Three UIs

To see a full-featured example that uses guidata and UserData data to share data among three UIs, use these steps to copy, run, and examine the code.

  1. Set your current folder to one to which you have write access.

  2. Copy the example code with this command

    copyfile(fullfile(docroot, 'techdoc','creating_guis',...
      'examples','guide*.*')),...
       fileattrib('guide*.*', '+w');
    
  3. Display the UIs in three GUIDE Layout Editors with these commands:

    guide guide_iconeditor; 
    guide guide_toolpalette; 
    guide guide_colorpalette;
  4. View the code in the editor by clicking the Editor button, , in each Layout Editor.

  5. Run the guide_iconeditor program by clicking Run Figure button, , in that UI's Layout Editor.

More About

Was this topic helpful?