What is the difference between application data, GUIDATA, and UserData in MATLAB?

40 views (last 30 days)
I would like to know what to use to store information in my GUI, or use to pass information between GUIs, application data (set/get with SETAPPDATA or GETAPPDATA), GUIDATA (set/get with GUIDATA), and UserData (set/get with SET or GET).

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Sep 2013
Application data is data that is meaningful to or defined by your application, which you attach by name to a figure or a graphics object within the figure in MATLAB. Functions SETAPPDATA and GETAPPDATA are (respectviely) used for storage and retrieval of application data. For example:
h = plot(1:10);
setappdata(h,'FirstAppData',magic(9));
setappdata(h,'SecondAppData',{'A string'});
first = getappdata(h,'FirstAppData');
second = getappdata(h,'SecondAppData');
UserData is a property that all graphics objects in MATLAB possess, and can be used to store any single, user-defined array with a particular object. While you cannot access an application data unless you know its name, any user-defined data (if it exists) can always be accessed using 'UserData' property with SET and GET methods.
h = surf(peaks);
set(h,'UserData',rand(5));
ud = get(h,'UserData');
GUIDATA is a function used to associate data with the figure containing the GUI. GUIDATA can manage only one variable at a time and hence the variable is usually defined to be a structure with mutliple fields. GUIDATA is commonly used to share handles to different sub-components of the GUI between the various callbacks in the GUI. GUIDE uses GUIDATA similarly to store and maintain the handles structure.
Internally, GUIDATA actually uses a particular entry in the application data to store the information. This can be seen by viewing the contents of GUIDATA:
edit guidata
---------------------------guidata.m----------------------------
% choose a unique name for our application data property.
% This file should be the only place using it.
PROP_NAME = 'UsedByGUIData_m';
% ...SNIP...
if nargin == 1 % GET
data = getappdata(fig, PROP_NAME);
else % (nargin == 2) SET
setappdata(fig, PROP_NAME, data_in);
end
In general, GUIDATA should be used to store and access information from the 'handles' structure, especially in GUIDE-generated GUIs. Saving large amounts of data in the 'handles' structure can sometimes cause a considerable slowdown, especially if GUIDATA is often called within the various sub-functions of the GUI. For this reason, it is recommended to use the 'handles' structure only to store handles to graphics objects. For other kinds of data, SETAPPDATA and GETAPPDATA should be used to store it as application data.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!