Blank GUI figure handle? (noob question)

조회 수: 4 (최근 30일)
thomas mann
thomas mann 2013년 8월 27일
sorry for this noob question, i just started to learn about GUI making, but i cannot figure out this:
when i create a new blank GUI using GUIDE, an .m file is generated. i cannot find inside this .m file the handle of the blank figure. callback functions of all gui elements (e.g. push buttons) are pointing to the same handles "hObject".
so what is the handle of the blank figure, and what are the handles of added gui elements?

채택된 답변

Jan
Jan 2013년 8월 27일
편집: Jan 2013년 8월 27일
Although it might look confusing, the situation is clear and surely will become clear to you soon. You can create a button programatically or by guide. Both are equivalent, when you write:
handles.myButton1 = uicontrol('Style', 'PushButton', 'Tag', 'myButton1', ...
'Callback', @myButton1Callback);
Now the callback of the button is called as:
function myButton1Callback(hObject, EventData)
And inside this function you can obtain the current version of the handles struct easily:
handles = guidata(hObject);
GUIDE appends the handles struct as 3rd input automatically already, such that you get:
function myButton1Callback(hObject, EventData, handles)
But I'm not sure, if this handles struct is the newest version already (I'm not working with GUIDE), but the guidata method shown above will work also.
Each object in a GUI has its own handles, which can be seen as a pointer or address of the GUI object. In all callbacks, the handle of the activated object is called "hObject", but you can modify the name of the variable freely:
function myButton1Callback(ButtonHandle, EventData)
set(ButtonHandle, 'String', 'I am pressed');

추가 답변 (3개)

Vishal Rane
Vishal Rane 2013년 8월 27일
The hObject is the handle to the object/component triggering the callback. Also the gui/figure handle would be probably in handles.figure. The handles structure contains handles of all the gui components and the gui itself.
  댓글 수: 1
thomas mann
thomas mann 2013년 8월 27일
thank you for your answer, yes, thats true, the handle of the GUI is 'hObject', because if i add set(hObject,'Color',[1,0.4,0.6]) to the .m code, it works! BUT!
if i add 2 buttons via GUIDE, it creates 2 callback functions inside .m file:
function pushbutton1_Callback(hObject, eventdata, handles) and function pushbutton2_Callback(hObject, eventdata, handles)
and both callbacks have the same handles 'hObject'. so if i want to change backgoundcolor of button nr. 2 by pressing button nr. 1, i put
set(hObject,'BackgroundColor',[1,0.4,0.6])
under callback of button nr.1. This changes bckcolor of button nr.1 , not nr.2! i want to point to handle of button nr.2. like
set(handles.pushbutton2,'BackgroundColor',[1,0.4,0.6]) which actually works
but the problem is, if i add a pushbutton via GUIDE, it places only a callback to .m file , but it does not add a button with handle like
handleofpushbutton2 = uicontrol
so i cannot modify handle handles.pushbutton2 to my own when its added to gui.

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


Image Analyst
Image Analyst 2013년 8월 27일
In GUIDE, double click on the figure to bring up the property inspector. Look at the "Tag" property - that is the handle name of the figure. For example, if the tag is figMainWindow, then in your code you use handles.figMainWindow whenever you need to refer to the main window (GUI) that contains all of your other controls (listboxes, radio buttons, etc.). If you add a push button with the tag "Go", then you will refer to handles.Go.
  댓글 수: 1
thomas mann
thomas mann 2013년 8월 27일
thank you for your answer, yes, handles.objectTag is the handle, but it is possible to modify from the code the handle, when the uicontorl is added to the gui? like this:
andleofpushbutton2 = uicontrol
please see my answer above for Vishal Rane

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


thomas mann
thomas mann 2013년 8월 27일
편집: thomas mann 2013년 8월 27일
let me clear up the things:
the reason im confused is, when i add an uicontrol by coding to the .m file, i can explicitly specify the handle of that button like this
myhandle = uicontrol
and then calling back to a function when clicked:
function callback(myhandle,...)
BUT when i place a button via GUIDE, there is no 'myhandle = uicontorl' added to .m file, so i cannot change the handle from the .m file. The handle is already given as handles.tagofthebutton and the callback as function tagofthebutton_callback(hObject,...)
where hObject does NOT specify the handle of a given or different button, but always points to itself. am i right?
  댓글 수: 1
Jan
Jan 2013년 8월 27일
편집: Jan 2013년 8월 27일
Please do not post clarifications in the answers section. If new information are helpful to understand the question, they should appear inside the question, e.g. marked by "[EDITED]" to show readers, that a addition has been inserted.

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by