Basic how to use guidata??

23 vues (au cours des 30 derniers jours)
kofort
kofort le 26 Avr 2017
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 commentaire
Adam
Adam le 26 Avr 2017
The help documentation is always the best place to start:

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 26 Avr 2017
Modifié(e) : Jan le 26 Avr 2017
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 commentaires
Jan
Jan le 26 Avr 2017
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 le 2 Déc 2019
Excellent answer, Jan. Thank you.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Word games dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by