Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Programmatic GUI pushbutton callback
Date: Thu, 26 Jun 2008 18:15:03 +0000 (UTC)
Organization: Bedford Institute of Oceanography
Lines: 100
Message-ID: <g40mb7$i96$1@fred.mathworks.com>
References: <g40e4n$aiq$1@fred.mathworks.com> <g40jj2$f7m$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1214504103 18726 172.30.248.37 (26 Jun 2008 18:15:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 26 Jun 2008 18:15:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1149396
Xref: news.mathworks.com comp.soft-sys.matlab:476070



"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.