What does guidata do?
7 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 19 Nov 2018
Edited: Stephen23
on 19 Nov 2018
" 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.
Accepted Answer
Rik
on 18 Nov 2018
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 Comments
Rik
on 19 Nov 2018
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.
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!