Basic how to use guidata??
22 次查看(过去 30 天)
显示 更早的评论
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
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
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!