Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Share Data Among Callbacks

Overview of Data Sharing Techniques

Many GUIs 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 GUI that need to access it. The table below describes several different methods for sharing data within your GUI.

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

Use set and get to store or retrieve the value of an object property.

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

  • Requires access to the object handle to set or retrieve the object 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 an object handle to set or retrieve the application data.

  • Can share multiple variables.

Create Nested Callback Functions (Programmatic GUIs)

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

  • Requires that functions be coded in the same file as the main GUI code.

  • Not recommended for GUIDE GUIs.

  • Can share multiple variables.

Store Data Using the guidata Function

Share data with the GUI figure 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 the component handle. If you do not have a handle to the component, then use the findobj function to search for a handle that has a known property value. You can store any data in the UserData property of a UI component or figure. For example, the following programmatic GUI 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 = get(hObject,'Value');
	diffMax = get(hObject,'Max') - sval;
	data = struct('val',sval,'diffMax',diffMax);
	set(hObject,'UserData',data);
end

function button_callback(hObject,eventdata)
	h = findobj('Tag','slider1');
	data = get(h,'UserData');
	display([data.val data.diffMax]);
end

When the end 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 end user clicks the push button, the button_callback function finds the handle to 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 GUIs.

Store Data as Application Data

You can share data in a GUI using a facility called application data. To store application data, you call the setappdata function:

setappdata(h,name,value);

The first input, h, is the handle to the component with which to associate the data. The second input, name, is 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(h,name);

The handle, h, must be a handle to the component associated with the data. The second input, name, must match the string you used to store the data. Unlike UserData, which only stores only one variable, application data can accommodate multiple variables.

The following programmatic GUI 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)
	sval = get(hObject,'Value');
	diffMax = get(hObject,'Max') - sval;
	hfig = get(hObject,'Parent');
	setappdata(hfig,'slidervalue',sval);
	setappdata(hfig,'difference',diffMax);
end

function button_callback(hObject,eventdata)
	hfig = get(hObject,'Parent');
	currentval = getappdata(hfig,'slidervalue');
	diffval = getappdata(hfig,'difference');
	display([currentval diffval]);
end

When the end user moves the slider, the slider_callback function calls setappdata to save the new slider value as application data using the name, 'slidervalue'. The next line of code calls setappdata to save another value, diffMax, using the name, 'difference'. Both calls to setappdata associate the data with the parent object, which is the GUI figure in this case.

When the end 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.

The setappdata and getappdata syntaxes are the same for GUIDE and programmatic GUIs.

Create Nested Callback Functions (Programmatic GUIs)

You can nest callback functions inside the main function of a programmatic GUI. 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 GUI handles 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 = get(hObject,'Value');
	 	diffMax = get(hObject,'Max') - 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 end 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 GUIs.

Store Data Using the guidata Function

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

guidata(object_handle,data);

The first input, object_handle, is a handle to any component in the GUI (typically hObject). The second input, data, is the variable to store. Every time you call guidata using two input arguments, it 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 handle you specify to store the data does not need to be the same handle 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 GUI

To use guidata in a programmatic GUI, 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 GUI 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 = get(hObject,'Value');
	data.diffMax = get(hObject,'Max') - data.val;
	guidata(hObject,data);
end

function button_callback(hObject,eventdata)
	data = guidata(hObject);
	display([data.val data.diffMax]);
end

When the end 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 GUI

GUIDE uses guidata to store a struct array called handles, which contains all the handles in the GUI. 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 GUI 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 GUI by clicking the Run Figure button, .

Sharing Data Among Multiple GUIDE GUIs

Example of Sharing Data Between Two GUIs

To see a full-featured example that uses application data and the guidata function to share data between two separate GUIs, 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 GUIs 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 GUI by clicking Run Figure button, , in that GUI Layout Editor.

Example of Sharing Data Among Three GUIs

To see a full-featured example that uses guidata and UserData data to share data among three GUIs, 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 GUIs 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 GUI by clicking Run Figure button, , in that GUI Layout Editor.

More About

Was this topic helpful?