What does guidata do?

7 次查看(过去 30 天)
Andrew Landau
Andrew Landau 2018-11-18
编辑: Stephen23 2018-11-19
I'm using an older version of Matlab - R2009b. In my callbacks, I often call something like:
set(handles.tag1,'Value',5)
guidata(hObject,handles);
% ^This seems unnecessary because I'm accessing the handle to tag1 directly
According to current documentation, I should follow this up with guidata, but I haven't identified a place where this actually matters. I think I'm bypassing the need to call guidata because I'm setting the handle directly.
I think this wouldn't be the case in a later version of matlab, in which handles is a complete structure. In the newest version of matlab I'd write this:
handles.tag1.Value = 5;
guidata(hObject,handles);
% ^Now necessary because the handles structure is contained in this workspace
Now, the changed value to handles.tag1.Value only affects the current workspace (inside my callback) unless I either make handles an output of the callback or call guidata. I'm new to making GUIs with guide so I just want to make sure I understand these functions correctly. Can someone confirm my understanding is correct, or explain what's really going on?
Thank you.
  1 个评论
Stephen23
Stephen23 2018-11-19
编辑:Stephen23 2018-11-19
" I should follow this up with guidata, but I haven't identified a place where this actually matters."
It only matters if you are actually changing the data inside handles. Neither of your examples do this, so calling guidata at the end of those callbacks is not required. The only time it is required is when you actually change something inside the handles structure itself, e.g.:
handles.mydata = 1234
and you want that data available from within other callbacks in your GUI.
"What does guidata do?"
It stores whatever variable you give it in the parent figure of the handle that you give it. When using GUIDE the variable is handles and the parent figure is the GUI figure. Actually the variable does not have to be handles, but of course your GUI won't work if you replace handles with the string 'hello world', so storing another variable there is only of interest if you want to stop your GUI from working.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2018-11-18
It actually works as you described it
Guidata stores a struct to your GUI, or loads a saved struct. If you make changes you need to save them. Neither of the syntaxes you describe require saving, because you are modifying object properties, not the struct contents.
  2 个评论
Andrew Landau
Andrew Landau 2018-11-18
hm, so in the second group of code I wrote I'd need to call guidata, right? Otherwise I'm just modifying struct contents.
Rik
Rik 2018-11-19
The second syntax may look like you are modifying the struct, but actually you are not. Because you are modifying the object instead of the struct you don't need to call guidata. If you were adding fields or modifying your own fields, then you would need to call guidata to update the stored data.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2009b

Community Treasure Hunt

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

Start Hunting!

Translated by