Basic how to use guidata??

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 日

3 投票

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 件のコメント

kofort
kofort 2017 年 4 月 26 日
function PUZZLEGUI_OpeningFcn(hObject, eventdata, handles, varargin)
PUZZLE
handles.output = hObject;
guidata(hObject, handles);
function varargout = PUZZLEGUI_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function moveleft_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
handles.output = hObject;
guidata(hObject, handles);
PUZZLE;Moveleft;
function moveright_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
handles.output = hObject;
guidata(hObject, handles);
MoveRight;MoveRight;MoveRight
This is my code
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeWord games についてさらに検索

タグ

質問済み:

2017 年 4 月 26 日

コメント済み:

2019 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by