Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Using workspace variable in m-file
Date: Sun, 7 Sep 2008 22:33:40 -0400
Organization: The MathWorks, Inc.
Lines: 41
Message-ID: <ga22u4$db2$1@fred.mathworks.com>
References: <g9t86u$cu5$1@fred.mathworks.com> <g9td2h$se1$1@fred.mathworks.com> <g9tg9l$55$1@fred.mathworks.com> <51dfc6c1-996a-4a79-a957-218aca2d1ad9@d77g2000hsb.googlegroups.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1220841220 13666 144.212.105.187 (8 Sep 2008 02:33:40 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 8 Sep 2008 02:33:40 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
Xref: news.mathworks.com comp.soft-sys.matlab:489090




"ImageAnalyst" <imageanalyst@mailinator.com> wrote in message 
news:51dfc6c1-996a-4a79-a957-218aca2d1ad9@d77g2000hsb.googlegroups.com...
> On Sep 6, 4:51 am, "John D'Errico" <woodch...@rochester.rr.com> wrote:
> > "Husam Aldahiyat" <numand...@gmail.com> wrote in message 
> > <g9td2h$se...@fred.mathworks.com>...
> > > Use global. Solves all life's problems.
> >
> > Yes, and it creates other problems. Globals
> > are sloppy, lazy programming.
> >
> > Pass the variables in. If they are functions,
> > perhaps anonymous functions, or simply
> > variables, then it is far simpler to pass them
> > in as arguments to the function. This solves
> > the OP's problems without creating other
> > ones.
> >
> > John
>
> -------------------------------------------
> John:
> What if the function is the callback of a button (placed with GUIDE)?
> How do you pass variables into that?  GUIDE automatically puts in 3
> arguments.  How can you get in other variables that may be needed
> inside that function?  I've just been using "global" because I don't
> know any other way to get stuff in there.  For a simple example, let's
> say the button should multiply a vector by 2 and plot in in an axes.
> How do you get that vector into the callback function of the button?

In this case, you can't pass variables into the callback directly, but there 
are better ways to handle that situation than using globals -- the usual 
suspects are storing values in the UserData or the handles structure and 
retrieving them inside the callback function, writing your callback as a 
nested functions, using SETAPPDATA/GETAPPDATA, etc.

-- 
Steve Lord
slord@mathworks.com