Difference between setappdata and guidata?

2 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2011-7-14
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.)

更多回答(1 个)

Doug Eastman
Doug Eastman 2011-7-14
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 个评论
Walter Roberson
Walter Roberson 2011-7-15
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by