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:
Programmatic GUI pushbutton callback

Subject: Programmatic GUI pushbutton callback

From: Kris Ezra

Date: 26 Jun, 2008 15:55:03

Message: 1 of 8


I am building a GUI to interface with a MATLAB model and,
in the interest of maintaining GUI variables in the
workspace and accessible, I have opted to create it
programmatically and not in function form instead of with
GUIDE.

The problem I have is that, while I can easily create the
GUI and push buttons with code like

startbutton =
uicontrol(panel3,'Style','pushbutton','String','Create
Scenario',...
    'Position',[40, 80, 120, 40], 'BackgroundColor',
[0.8,0.8,0.8]);

I cannot find any way to set the button callback. I would
rather not use custom functions (which shouldnt matter
since the main GUI is just written in a script file) and I
cant find a solution.

As a simple test, I would like to have "startbutton" open a
PDF file.

I have tried something to the effect of set
(startbutton, 'Callback', open usersguide.pdf) but this
does not work. What else can I do to code button callbacks?

Subject: Programmatic GUI pushbutton callback

From: matt dash

Date: 26 Jun, 2008 16:10:22

Message: 2 of 8

"Kris Ezra" <sharpshooter1405@yahoo.com> wrote in message
<g40e4n$aiq$1@fred.mathworks.com>...
>
> I am building a GUI to interface with a MATLAB model and,
> in the interest of maintaining GUI variables in the
> workspace and accessible, I have opted to create it
> programmatically and not in function form instead of with
> GUIDE.
>
> The problem I have is that, while I can easily create the
> GUI and push buttons with code like
>
> startbutton =
> uicontrol(panel3,'Style','pushbutton','String','Create
> Scenario',...
> 'Position',[40, 80, 120, 40], 'BackgroundColor',
> [0.8,0.8,0.8]);
>
> I cannot find any way to set the button callback. I would
> rather not use custom functions (which shouldnt matter
> since the main GUI is just written in a script file) and I
> cant find a solution.
>
> As a simple test, I would like to have "startbutton" open a
> PDF file.
>
> I have tried something to the effect of set
> (startbutton, 'Callback', open usersguide.pdf) but this
> does not work. What else can I do to code button callbacks?

try putting the callback in single quotes to make it a string...

Subject: Programmatic GUI pushbutton callback

From: matt dash

Date: 26 Jun, 2008 16:15:04

Message: 3 of 8

"Kris Ezra" <sharpshooter1405@yahoo.com> wrote in message
<g40e4n$aiq$1@fred.mathworks.com>...
>
> I am building a GUI to interface with a MATLAB model and,
> in the interest of maintaining GUI variables in the
> workspace and accessible, I have opted to create it
> programmatically and not in function form instead of with
> GUIDE.
>
> The problem I have is that, while I can easily create the
> GUI and push buttons with code like
>
> startbutton =
> uicontrol(panel3,'Style','pushbutton','String','Create
> Scenario',...
> 'Position',[40, 80, 120, 40], 'BackgroundColor',
> [0.8,0.8,0.8]);
>
> I cannot find any way to set the button callback. I would
> rather not use custom functions (which shouldnt matter
> since the main GUI is just written in a script file) and I
> cant find a solution.
>
> As a simple test, I would like to have "startbutton" open a
> PDF file.
>
> I have tried something to the effect of set
> (startbutton, 'Callback', open usersguide.pdf) but this
> does not work. What else can I do to code button callbacks?


I should be more specific:

The "best" thing to do is this:

 uicontrol(panel3,'Style','pushbutton','String','Create
 Scenario',...
     'Position',[40, 80, 120, 40], 'BackgroundColor',
 [0.8,0.8,0.8],...
'callback',@button1_callback);

and at the end of your file:


function button1_callback(src,eventdata)
hanldles=guidata(src)

open usersguide.pdf

guidata(src,handles)

Subject: Programmatic GUI pushbutton callback

From: Kris Ezra

Date: 26 Jun, 2008 17:28:02

Message: 4 of 8

"Kris Ezra" <sharpshooter1405@yahoo.com> wrote in message
<g40e4n$aiq$1@fred.mathworks.com>...
>
> I am building a GUI to interface with a MATLAB model and,
> in the interest of maintaining GUI variables in the
> workspace and accessible, I have opted to create it
> programmatically and not in function form instead of with
> GUIDE.
>
> The problem I have is that, while I can easily create the
> GUI and push buttons with code like
>
> startbutton =
> uicontrol(panel3,'Style','pushbutton','String','Create
> Scenario',...
> 'Position',[40, 80, 120, 40], 'BackgroundColor',
> [0.8,0.8,0.8]);
>
> I cannot find any way to set the button callback. I
would
> rather not use custom functions (which shouldnt matter
> since the main GUI is just written in a script file) and
I
> cant find a solution.
>
> As a simple test, I would like to have "startbutton" open
a
> PDF file.
>
> I have tried something to the effect of set
> (startbutton, 'Callback', open usersguide.pdf) but this
> does not work. What else can I do to code button
callbacks?


The idea of keeping the callback in single quotes
definitely worked for this specific example; however,
because I'm writing the gui in a script file I cannot
define functions at the end so the second idea is out. Is
there another option besides creating custom external
functions for EVERY uicontrol item I write (besides some
complicated flagging system that I could write to do the
functionality?

I would like to find a way to do callbacks contained in the
script file that conduct lengthy operations. For example,
I may have a next button that calls a giant switch to see
what panels to set as visible or invisible wich I would
rather not have to put in single quotes inside the button
definition :).

Thanks!

Kris

Subject: Programmatic GUI pushbutton callback

From: matt dash

Date: 26 Jun, 2008 17:46:02

Message: 5 of 8

"Kris Ezra" <sharpshooter1405@yahoo.com> wrote in message
<g40jj2$f7m$1@fred.mathworks.com>...
> "Kris Ezra" <sharpshooter1405@yahoo.com> wrote in message
> <g40e4n$aiq$1@fred.mathworks.com>...
> >
> > I am building a GUI to interface with a MATLAB model and,
> > in the interest of maintaining GUI variables in the
> > workspace and accessible, I have opted to create it
> > programmatically and not in function form instead of with
> > GUIDE.
> >
> > The problem I have is that, while I can easily create the
> > GUI and push buttons with code like
> >
> > startbutton =
> > uicontrol(panel3,'Style','pushbutton','String','Create
> > Scenario',...
> > 'Position',[40, 80, 120, 40], 'BackgroundColor',
> > [0.8,0.8,0.8]);
> >
> > I cannot find any way to set the button callback. I
> would
> > rather not use custom functions (which shouldnt matter
> > since the main GUI is just written in a script file) and
> I
> > cant find a solution.
> >
> > As a simple test, I would like to have "startbutton" open
> a
> > PDF file.
> >
> > I have tried something to the effect of set
> > (startbutton, 'Callback', open usersguide.pdf) but this
> > does not work. What else can I do to code button
> callbacks?
>
>
> The idea of keeping the callback in single quotes
> definitely worked for this specific example; however,
> because I'm writing the gui in a script file I cannot
> define functions at the end so the second idea is out. Is
> there another option besides creating custom external
> functions for EVERY uicontrol item I write (besides some
> complicated flagging system that I could write to do the
> functionality?
>
> I would like to find a way to do callbacks contained in the
> script file that conduct lengthy operations. For example,
> I may have a next button that calls a giant switch to see
> what panels to set as visible or invisible wich I would
> rather not have to put in single quotes inside the button
> definition :).
>
> Thanks!
>
> Kris

Well I don't know about that, but is there any reason you
can't turn your script file into a function file just by
putting the line:

function myguiname

at the top? That would let you define more functions in the
file at the end. I do this all the time. But even with the
script I think you should be able to do what I said and just
put the callbacks in functions in their own, separate files.

Subject: Programmatic GUI pushbutton callback

From: Yuri Geshelin

Date: 26 Jun, 2008 18:15:03

Message: 6 of 8

"Kris Ezra" <sharpshooter1405@yahoo.com> wrote in message
<g40jj2$f7m$1@fred.mathworks.com>...
> "Kris Ezra" <sharpshooter1405@yahoo.com> wrote in message
> <g40e4n$aiq$1@fred.mathworks.com>...
> >
> > I am building a GUI to interface with a MATLAB model
and,
> > in the interest of maintaining GUI variables in the
> > workspace and accessible, I have opted to create it
> > programmatically and not in function form instead of
with
> > GUIDE.
> >
> > The problem I have is that, while I can easily create
the
> > GUI and push buttons with code like
> >
> > startbutton =
> > uicontrol(panel3,'Style','pushbutton','String','Create
> > Scenario',...
> > 'Position',[40, 80, 120, 40], 'BackgroundColor',
> > [0.8,0.8,0.8]);
> >
> > I cannot find any way to set the button callback. I
> would
> > rather not use custom functions (which shouldnt matter
> > since the main GUI is just written in a script file)
and
> I
> > cant find a solution.
> >
> > As a simple test, I would like to have "startbutton"
open
> a
> > PDF file.
> >
> > I have tried something to the effect of set
> > (startbutton, 'Callback', open usersguide.pdf) but this
> > does not work. What else can I do to code button
> callbacks?
>
>
> The idea of keeping the callback in single quotes
> definitely worked for this specific example; however,
> because I'm writing the gui in a script file I cannot
> define functions at the end so the second idea is out.
Is
> there another option besides creating custom external
> functions for EVERY uicontrol item I write (besides some
> complicated flagging system that I could write to do the
> functionality?
>
> I would like to find a way to do callbacks contained in
the
> script file that conduct lengthy operations. For
example,
> I may have a next button that calls a giant switch to see
> what panels to set as visible or invisible wich I would
> rather not have to put in single quotes inside the button
> definition :).
>
> Thanks!
>
> Kris

Kris,

First, I totally agree with matt dash's last post, in fact,
I was typing up the same concepts, but he came ahead of me.
Second, you do not like the idea of having a function per
button. Fair enough. Here is what you can do.

Suppose you have 4 buttons, whose handles make up an array h
(1:4)

for i = 1 : 4
   set(h(i),'callback',{@my_funct,i})
end

The header of my_funct (whether nested or separate) will
then be:

function my_funct(src,eventdata,button_number)

And then it's up to you to write "switch button_number" and
do what you like.

Alternatively, callback can be implemented in this way:

set(h(i),'callback',['my_funct(' num2str(i) ')'])

In this case, you need only one argument in my_funct:
button_number. But in a more complicate case better use
function handles.

Yuri

PS I did not test these particular commands, but the
approach used to work out for me many times.

Subject: Programmatic GUI pushbutton callback

From: Kris Ezra

Date: 26 Jun, 2008 18:43:06

Message: 7 of 8


Thanks! Between the two of you I think I have it figured
out. The issue is that by turning the gui script into a
function, I have scope problems interfacing with the model
script it has to interface with. After talking with my
colleagues on the project we will just use multiple custom
functions for callbacks.

As a last question, is there any reason why I should be
including the guidata(src,handles) style internal saves in
these functions? I deleted this and the "handles = get
(whatever) from the first code and it still works it
appears. I understand that when you are using GUIDE this
updates the handles structure so if you are carrying
variables they actually get changed, but if I'm just
setting properties (i.e., visibility on and off) is this
useful/necessary?

Subject: Programmatic GUI pushbutton callback

From: matt dash

Date: 26 Jun, 2008 19:02:01

Message: 8 of 8


>
> As a last question, is there any reason why I should be
> including the guidata(src,handles) style internal saves in
> these functions? I deleted this and the "handles = get
> (whatever) from the first code and it still works it
> appears. I understand that when you are using GUIDE this
> updates the handles structure so if you are carrying
> variables they actually get changed, but if I'm just
> setting properties (i.e., visibility on and off) is this
> useful/necessary?

No, you only need to load "handles" at the start of
functions that will need to reference it, and save "handles"
at the end of functions that change it. So if a callback
sets properties of objects whose handles are stored in
"handles", you will need to load it to reference them, but
you won't need to save it. I put those in the example
because (if you are using handles at all) it never hurts to
have them there.

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