Difference between setappdata and guidata?
2 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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.)
0 Comments
More Answers (1)
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
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);
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!