Filter löschen
Filter löschen

Difference between setappdata and guidata?

1 Ansicht (letzte 30 Tage)
B_Richardson
B_Richardson am 14 Jul. 2011
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?

Akzeptierte Antwort

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

Weitere Antworten (1)

Doug Eastman
Doug Eastman am 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 Kommentare
Walter Roberson
Walter Roberson am 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);
B_Richardson
B_Richardson am 15 Jul. 2011
I see! Thank you!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by