Difference between setappdata and guidata?

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?

 采纳的回答

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 个)

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 个评论

I see. I have an array:
A = zeros(10,length(get(listbox1,'string')));
Originally I was initializing it in a pushbutton callback, however throughout the program I want to change the array instead of creating a new one each time the button is created. So I am attempting to move that line into the createfunction. That is why I asked the question.
So I would need to save A into guidata? The syntax for doing this is confusing to me though. Can you help me with this problem?
handles = guidata(gcf);
if ~isfield(handles, 'A')
handles.A = zeros(10,length(get(listbox1,'string')));
guidata(gcf, handles);
end
If it wouldn't be too much trouble, would you mind explaining this code? I am trying to gain a better understanding of how data is handled in GUIDE.
handles = guidata(gcf); %creates a new instance of guidedata and stores it in handles?
if ~isfield(handles, 'A') %no idea what this does?
handles.A = zeros(10,length(get(listbox1,'string'))); %store A in handles
guidata(gcf, handles); %saves A? I think?
end
So now throughout the program if I wanted to use A I would just call handles.A right?
R = get(handles.popupmenu5,'val');
C = get(handles.listbox3,'val');
handles.A(R,C) = 1;
Is that correct syntax?
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)
Wow, this is the most complete explanation of handles that I have ever heard or read! Matlab should hire you to write the HELP sections!
One last quick question just to make sure I understand correctly:
%if I wanted to update A in a callback from a gui component:
handles.A = zeros(10,length(get(handles.listbox3,'string')));
%or
set(handles.A, zeros(10,length(get(handles.listbox3,'string'))));
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);

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Graphics Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by