Difference between setappdata and guidata?
Mostrar comentarios más antiguos
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?
Respuesta aceptada
Más respuestas (1)
Doug Eastman
el 14 de Jul. de 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 comentarios
B_Richardson
el 14 de Jul. de 2011
Walter Roberson
el 14 de Jul. de 2011
handles = guidata(gcf);
if ~isfield(handles, 'A')
handles.A = zeros(10,length(get(listbox1,'string')));
guidata(gcf, handles);
end
B_Richardson
el 14 de Jul. de 2011
Walter Roberson
el 15 de Jul. de 2011
handles = guidata(gcf)
is the same as
handles = getappdata(gcf,'UsedByGUIData_m')
which means to fetch the application data variable named UsedByGUIData_m that has been stored for the current figure (gcf returns the current figure number)
isfield(handles,'A') means to check if there is already a field in the structure "handles" that is named "A". This test is then negated, so the body of the "if" will only be executed if there is NOT already a field named "A" in the structure. This is the presumption that if there is already an "A" it should be left alone rather than being written over.
guidata(gcf,handles)
is then the same as
setappdata(gcf,'UsedByGUIData_m', handles)
which sets the current figure's application data variable named UsedByGUIData_m to whatever contents are in the variable "handles".
In any routine that already has "handles" passed to it, handles.A will access A. If the routine does not already have "handles" passed to it, you would use
handles = guidata(gcf)
in order to retrieve the entire "handles" structure.
You can modify handles.A as you like, but keep in mind that "handles" itself is going to be a local variable, so any time some other routine is going to need to use the updated value of "A", you need to use
guidata(gcf, handles)
to update the application data's copy of the "handles" structure.
guidata(gcf) gives you a current _copy_ of the application data; you can change that copy all you like, but the application data itself will not be updated until you specifically write a new value to it with guidata(gcf, handles)
B_Richardson
el 15 de Jul. de 2011
B_Richardson
el 15 de Jul. de 2011
Walter Roberson
el 15 de Jul. de 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
el 15 de Jul. de 2011
Categorías
Más información sobre Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!