Basic how to use guidata??

조회 수: 21 (최근 30일)
kofort
kofort 2017년 4월 26일
댓글: Kristoffer Walker 2019년 12월 2일
I'm really new to MATLAB so I apologise if the question is silly. Basically, I need my GUI to save the data from the previous command and build upon the figure that I have. I'm essentially building a puzzle and my push buttons are the different moves that I have coded as .m files. I can't manage to get the figure to keep all the previous moves and add a new one. Instead, it just goes back to the original layout. I dont understand what i sub into 'guidata(hObject, handles);'. I dont really understand what a handle is, and I've read that I have to replace 'hObject' with the tag name of my figure(axes) or with the file name of my gui- these attempts caused all sorts of problems..
Can anyone break down an examples for me??

답변 (1개)

Jan
Jan 2017년 4월 26일
편집: Jan 2017년 4월 26일
A "handle" is a kind of pointer to address a graphics object. It is replied, when such an object is created:
buttonHandle = uicontrol('Style', 'PushButton');
Afterwards you can use the handle to access the properties:
set(buttonHandle, 'String', 'Hello')
get(buttonHandle, 'Value')
% Or in modern Matlab versions with the dot-notation:
buttonHandle.Position = [10, 10, 100, 30];
In GUIs created by GUIDE, the handles are store in a structed called "handles". You can stor other variables in this struct also, an therefore I consider the name of the variable as a really bad choice, because it is confusing.
As explained in the docs of guidata, this command reads and writes a variable to the ApplicationData of the figure:
handles = guidata(hObject); % Read the struct from the figure
handles.abc = rand % Modify the struct
guidata(hObject, handles); % Write the modified struct back to the figure
This is a cute way to share variables between callbacks. The variable hObject is teh first input to the callback and can be any graphic object of the GUI. Internally the handle of teh figure is determined automatically. There is not need to use another variable than hObject.
For future questions: Please post your code and explain "all sorts of problems" with details, preferrably the complete error message.
  댓글 수: 3
Jan
Jan 2017년 4월 26일
If you do not modify or use the handles struct, there is no need to call guidata:
function moveleft_Callback(hObject, eventdata, handles)
PUZZLE;
Moveleft;
function moveright_Callback(hObject, eventdata, handles)
MoveRight;
MoveRight;
MoveRight
Kristoffer Walker
Kristoffer Walker 2019년 12월 2일
Excellent answer, Jan. Thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Word games에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by