MATLAB Language MATLAB User Interfaces Passing Data Around User Interface

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Most advanced user interfaces require the user to be able to pass information between the various functions which make up a user interface. MATLAB has a number of different methods to do so.


guidata

MATLAB's own GUI Development Environment (GUIDE) prefers to use a struct named handles to pass data between callbacks. This struct contains all of the graphics handles to the various UI components as well as user-specified data. If you aren't using a GUIDE-created callback which automatically passes handles, you can retrieve the current value using guidata

% hObject is a graphics handle to any UI component in your GUI
handles = guidata(hObject);

If you want to modify a value stored in this data structure, you can modify but then you must store it back within the hObject for the changes to be visible by other callbacks. You can store it by specifying a second input argument to guidata.

% Update the value
handles.myValue = 2;

% Save changes
guidata(hObject, handles)

The value of hObject doesn't matter as long as it is a UI component within the same figure because ultimately the data is stored within the figure containing hObject.

Best for:

  • Storing the handles structure, in which you can store all the handles of your GUI components.
  • Storing "small" other variables which need to be accessed by most callbacks.

Not recommended for:

  • Storing large variables which do not have to be accessed by all callbacks and sub-functions (use setappdata/getappdata for these).

setappdata/getappdata

Similar to the guidata approach, you can use setappdata and getappdata to store and retrieve values from within a graphics handle. The advantage of using these methods is that you can retrieve only the value you want rather than an entire struct containing all stored data. It is similar to a key/value store.

To store data within a graphics object

% Create some data you would like to store
myvalue = 2

% Store it using the key 'mykey'
setappdata(hObject, 'mykey', myvalue)

And to retrieve that same value from within a different callback

value = getappdata(hObject, 'mykey');

Note: If no value was stored prior to calling getappdata, it will return an empty array ([]).

Similar to guidata, the data is stored in the figure that contains hObject.

Best for:

  • Storing large variables which do not have to be accessed by all callbacks and sub-functions.

UserData

Every graphics handle has a special property, UserData which can contain any data you wish. It could contain a cell array, a struct, or even a scalar. You can take advantage of this property and store any data you wish to be associated with a given graphics handle in this field. You can save and retrieve the value using the standard get/set methods for graphics objects or dot notation if you're using R2014b or newer.

% Create some data to store
mydata = {1, 2, 3};

% Store it within the UserData property
set(hObject, 'UserData', mydata)

% Of if you're using R2014b or newer:
% hObject.UserData = mydata;

Then from within another callback, you can retrieve this data:

their_data = get(hObject, 'UserData');

% Or if you're using R2014b or newer:
% their_data = hObject.UserData;

Best for:

  • Storing variables with a limited scope (variables which are likely to be used only by the object in which they are stored, or objects having a direct relationship to it).

Nested Functions

In MATLAB, a nested function can read and modify any variable defined in the parent function. In this way, if you specify a callback to be a nested function, it can retrieve and modify any data stored in the main function.

function mygui()    
    hButton = uicontrol('String', 'Click Me', 'Callback', @callback);

    % Create a counter to keep track of the number of times the button is clicked
    nClicks = 0;

    % Callback function is nested and can therefore read and modify nClicks
    function callback(source, event)
        % Increment the number of clicks
        nClicks = nClicks + 1;

        % Print the number of clicks so far
        fprintf('Number of clicks: %d\n', nClicks);
    end
end

Best for:

  • Small, simple GUIs. (for quick prototyping, to not have to implement the guidata and/or set/getappdata methods).

Not recommended for:

  • Medium, large or complex GUIs.

  • GUI created with GUIDE.


Explicit input arguments

If you need to send data to a callback function and don't need to modify the data within the callback, you can always consider passing the data to the callback using a carefully crafted callback definition.

You could use an anonymous function which adds inputs

% Create some data to send to mycallback
data = [1, 2, 3];

% Pass data as a third input to mycallback
set(hObject, 'Callback', @(source, event)mycallback(source, event, data))

Or you could use the cell array syntax to specify a callback, again specifying additional inputs.

set(hObject, 'Callback', {@mycallback, data})

Best for:

  • When the callback needs data to perform some operations but the data variable does not need to be modified and saved in a new state.



Got any MATLAB Language Question?