Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
GUI mouse button set handles

Subject: GUI mouse button set handles

From: Buuk

Date: 21 Apr, 2008 11:51:06

Message: 1 of 6

Hi everyone,

I am new to working with GUI and want to create an interface
to control 3 steppermotors for a 3D camera. In the GUI I
have some buttons that control the direction to move in.
These buttons should start the action when the mouse button
is down and stop when the mouse button is released.
I made a simple GUI to test this principle, but can't manage
it to set the handles for the button in the second phase.
Please see my code below.
Can anyone tell me how to solve this problem?

Thanks for your help!

Buuk

function togglebutton1_ButtonDownFcn(hObject, eventdata,
handles)

set(handles.togglebutton1,'String','Button pressed')
set(handles.togglebutton1,'Value',1.0)
set(handles.togglebutton1,'BackgroundColor',[.8,1,0])
disp('Button click works')

set(gcf,'WindowButtonUpFcn',@ButtonUpFcn);

function ButtonUpFcn(src,eventdata)
set(togglebutton1,'String',@buttonupfunction)
disp('test 2')
set(handles.togglebutton1,{'String'},'Button released');

Subject: GUI mouse button set handles

From: Vihang Patil

Date: 21 Apr, 2008 12:01:31

Message: 2 of 6

"Buuk " <skibuilders@gmail.com> wrote in message
<fuhv3a$n27$1@fred.mathworks.com>...
> Hi everyone,
>
> I am new to working with GUI and want to create an
interface
> to control 3 steppermotors for a 3D camera. In the GUI I
> have some buttons that control the direction to move in.
> These buttons should start the action when the mouse
button
> is down and stop when the mouse button is released.
> I made a simple GUI to test this principle, but can't
manage
> it to set the handles for the button in the second phase.
> Please see my code below.
> Can anyone tell me how to solve this problem?
>
> Thanks for your help!
>
> Buuk
>
> function togglebutton1_ButtonDownFcn(hObject, eventdata,
> handles)
>
> set(handles.togglebutton1,'String','Button pressed')
> set(handles.togglebutton1,'Value',1.0)
> set(handles.togglebutton1,'BackgroundColor',[.8,1,0])
> disp('Button click works')
>
> set(gcf,'WindowButtonUpFcn',@ButtonUpFcn);
>
> function ButtonUpFcn(src,eventdata)
> set(togglebutton1,'String',@buttonupfunction)
> disp('test 2')
> set(handles.togglebutton1,{'String'},'Button released');


Hello
Instead of using the ButtonDownFcn of the toggle button,
you can use instead the callback of the figure window. i.e
Right-Click on any empty space in the blank gui and View
its callback properties. In that you can use
the "WindowButtonDownFcn" and "WindowButtonUpFcn".
HTH
VIhang

Subject: GUI mouse button set handles

From: Buuk

Date: 21 Apr, 2008 17:36:02

Message: 3 of 6

"Vihang Patil" <vihang_patil@yahoo.com> wrote in message
<fuhvmr$bes$1@fred.mathworks.com>...
> "Buuk " <skibuilders@gmail.com> wrote in message
> <fuhv3a$n27$1@fred.mathworks.com>...
> > Hi everyone,
> >
> > I am new to working with GUI and want to create an
> interface
> > to control 3 steppermotors for a 3D camera. In the GUI I
> > have some buttons that control the direction to move in.
> > These buttons should start the action when the mouse
> button
> > is down and stop when the mouse button is released.
> > I made a simple GUI to test this principle, but can't
> manage
> > it to set the handles for the button in the second phase.
> > Please see my code below.
> > Can anyone tell me how to solve this problem?
> >
> > Thanks for your help!
> >
> > Buuk
> >
> > function togglebutton1_ButtonDownFcn(hObject, eventdata,
> > handles)
> >
> > set(handles.togglebutton1,'String','Button pressed')
> > set(handles.togglebutton1,'Value',1.0)
> > set(handles.togglebutton1,'BackgroundColor',[.8,1,0])
> > disp('Button click works')
> >
> > set(gcf,'WindowButtonUpFcn',@ButtonUpFcn);
> >
> > function ButtonUpFcn(src,eventdata)
> > set(togglebutton1,'String',@buttonupfunction)
> > disp('test 2')
> > set(handles.togglebutton1,{'String'},'Button released');
>
>
> Hello
> Instead of using the ButtonDownFcn of the toggle button,
> you can use instead the callback of the figure window. i.e
> Right-Click on any empty space in the blank gui and View
> its callback properties. In that you can use
> the "WindowButtonDownFcn" and "WindowButtonUpFcn".
> HTH
> VIhang

Thanks for your explanation, but I can't figure it out.
As you can see in my code above, I try to set the 'String'
handle of the togglebutton, but this doesn't work and I
can't get it to work using the "WindowButtonDownFcn" and
"WindowButtonUpFcn" commands. Is it possible to give an
example on how to menage this? Would be really apreciated.

Thanks, Buuk

Subject: GUI mouse button set handles

From: matt dash

Date: 21 Apr, 2008 17:54:02

Message: 4 of 6


> function togglebutton1_ButtonDownFcn(hObject, eventdata,
> handles)
>
> set(handles.togglebutton1,'String','Button pressed')
> set(handles.togglebutton1,'Value',1.0)
> set(handles.togglebutton1,'BackgroundColor',[.8,1,0])
> disp('Button click works')
>
> set(gcf,'WindowButtonUpFcn',@ButtonUpFcn);
>
> function ButtonUpFcn(src,eventdata)
> set(togglebutton1,'String',@buttonupfunction)
> disp('test 2')
> set(handles.togglebutton1,{'String'},'Button released');



1) In the last line there shouldn't be {} around 'string'
2) the line set(togglebutton1,'String',@buttonupfunction)
doesnt make any sense. You should delete it entirely.
3) your function ButtonUpFcn doesn't have handles as an
input, so it doesn't know what handles.togglebutton1 is.
4) I think you should use a pushbutton instead of toggle
button... it seems to make more sense for this application.
This way the button is displayed as pressed when you press
it and unpressed when you release it.



5) it is probably better to use the button's callback
instead of it's buttondownfcn.


try this instead:

 function togglebutton1_ButtonDownFcn(hObject, eventdata,
 handles)
 
 set(handles.togglebutton1,'String','Button pressed')
 set(handles.togglebutton1,'Value',1)
 set(handles.togglebutton1,'BackgroundColor',[.8,1,0])
 disp('Button click works')
 
 set(gcf,'WindowButtonUpFcn',{@ButtonUpFcn,handles});
 %handles is added as a 3rd input to your function
 % using the line above this and the line below this.
 function ButtonUpFcn(src,eventdata,handles)
 disp('test 2')
 set(handles.togglebutton1,'String','Button released');

Subject: GUI mouse button set handles

From: Buuk

Date: 21 Apr, 2008 18:13:02

Message: 5 of 6

"matt dash" <n.a@mail.com> wrote in message
<fuikbq$8el$1@fred.mathworks.com>...
>
> > function togglebutton1_ButtonDownFcn(hObject, eventdata,
> > handles)
> >
> > set(handles.togglebutton1,'String','Button pressed')
> > set(handles.togglebutton1,'Value',1.0)
> > set(handles.togglebutton1,'BackgroundColor',[.8,1,0])
> > disp('Button click works')
> >
> > set(gcf,'WindowButtonUpFcn',@ButtonUpFcn);
> >
> > function ButtonUpFcn(src,eventdata)
> > set(togglebutton1,'String',@buttonupfunction)
> > disp('test 2')
> > set(handles.togglebutton1,{'String'},'Button released');
>
>
>
> 1) In the last line there shouldn't be {} around 'string'
> 2) the line set(togglebutton1,'String',@buttonupfunction)
> doesnt make any sense. You should delete it entirely.
> 3) your function ButtonUpFcn doesn't have handles as an
> input, so it doesn't know what handles.togglebutton1 is.
> 4) I think you should use a pushbutton instead of toggle
> button... it seems to make more sense for this application.
> This way the button is displayed as pressed when you press
> it and unpressed when you release it.
>
>
>
> 5) it is probably better to use the button's callback
> instead of it's buttondownfcn.
>
>
> try this instead:
>
> function togglebutton1_ButtonDownFcn(hObject, eventdata,
> handles)
>
> set(handles.togglebutton1,'String','Button pressed')
> set(handles.togglebutton1,'Value',1)
> set(handles.togglebutton1,'BackgroundColor',[.8,1,0])
> disp('Button click works')
>
> set(gcf,'WindowButtonUpFcn',{@ButtonUpFcn,handles});
> %handles is added as a 3rd input to your function
> % using the line above this and the line below this.
> function ButtonUpFcn(src,eventdata,handles)
> disp('test 2')
> set(handles.togglebutton1,'String','Button released');
>
>

Thanks a lot for your clear help!
This gives me a better understanding in using handles!

Buuk

Subject: GUI mouse button set handles

From: mike

Date: 18 Feb, 2010 20:03:20

Message: 6 of 6

I use this to get button-down, button-up events working:

function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

disp('down')
set(hObject,'Value',1)

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

if get(handles.pushbutton1,'Value')==1 %add more conditions here to check which %button is pushed
    disp('up')
end
set(handles.pushbutton1,'Value',0)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us