What does guidata do?

조회 수: 5 (최근 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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2009b

Community Treasure Hunt

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

Start Hunting!

Translated by