Clear Filters
Clear Filters

Difference between setappdata and guidata?

5 views (last 30 days)
Hello everyone,
I have a quick question: What is the difference between setappdata and guidata functions? In what situation is one more appropriate to use than the other? And also if possible, can someone provide an example of both in use?
I have read the matlab help description and it seems that they can be used interchangeably?

Accepted Answer

Walter Roberson
Walter Roberson on 14 Jul 2011
guidata() is a wrapper around getappdata() and setappdata(), using the specific property name UsedByGUIData_m
guidata() was designed for use with GUIDE but can be used more generally.
guidata() is handy for treating the data as a group (such as for clearing it all prior to saving a figure), and is also handy for reducing the number of calls one might otherwise make to [gs]etappdata() if one is working with more than one property.
Some people prefer to separate the graphic object handles from the data or control variables; people who use guidata() tend to merge those together (and then tend to get confused about whether a particular name is the name of a handle or the name of an array or structure.)

More Answers (1)

Doug Eastman
Doug Eastman on 14 Jul 2011
You can look at the code for GUIDATA by typing
edit guidata
You'll notice that GUIDATA calls SETAPPDATA, it just provides some additional layers on top of it. Mainly it allows you to pass in the handle to any element in a figure and it also uses a hard-coded property name 'UsedByGUIData_m' that is removed before the figure is saved.
I would say in general if you are building GUIs especially created by GUIDE, you should be using GUIDATA, but if you are doing something more advanced and want to add your own arbitrary properties, you would use SETAPPDATA.
  8 Comments
Walter Roberson
Walter Roberson on 15 Jul 2011
Remember where I wrote above that, "people who use guidata() tend to merge those together (and then tend to get confused about whether a particular name is the name of a handle or the name of an array or structure.)"
Your handles.A is, despite its name, not a handle; if you had used setappdata() instead of guidata() you would have been more likely to keep the data types straight.
To unconditionally update A in a callback, you would use code like I showed above,
handles.A = zeros(10,length(get(handles.listbox3,'string')));
guidata(gcf, handles);

Sign in to comment.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!