Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to do an assignment in a matlab anonymous function?

976 views
Skip to first unread message

Nasser M. Abbasi

unread,
Jan 17, 2012, 8:39:30 PM1/17/12
to

ref:
http://www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html

Matlab anonymous only supports making an `expression`, which makes
them very restrictive. I can't do something as simple as this
silly example:

y=[0 0];
arrayfun(@(i) y(i)=i ,1:2)

and any other use of an anonymous function. Assignment to a variable is
not allowed.

(it is also not possible to do a logical test using an 'if' inside
anonymous function)

my question:
1. What is the recommended way to do assignments/updates to
variables from inside an anonymous function? I can think of
few, but was not sure if there is a know best method that
most use.

2. Why is this restriction there? Without this restriction, they
can become much more useful and powerful.

3. Why are they called 'functions' then? Since in a normal matlab
function, one can certainly do an assignment and use an if statement
in them?

thanks
--Nasser

Bruno Luong

unread,
Jan 18, 2012, 2:43:07 AM1/18/12
to
"Nasser M. Abbasi" <n...@12000.org> wrote in message <jf57sl$qhb$1...@speranza.aioe.org>...
>
> ref:
> http://www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html
>
> Matlab anonymous only supports making an `expression`, which makes
> them very restrictive. I can't do something as simple as this
> silly example:
>
> y=[0 0];
> arrayfun(@(i) y(i)=i ,1:2)
>
> and any other use of an anonymous function. Assignment to a variable is
> not allowed.
>
> (it is also not possible to do a logical test using an 'if' inside
> anonymous function)
>
> my question:
> 1. What is the recommended way to do assignments/updates to
> variables from inside an anonymous function? I can think of
> few, but was not sure if there is a know best method that
> most use.

I would return the question : why use anonymous function at all? May be nested function is better:

function y = test
y=[0 0];
function assigny(i)
y(i) = i;
end
arrayfun(@assigny ,1:2);

end

Personally I'm not fan of arrayfun and anonymous function. I prefer for-loop and regular function.

>
> 2. Why is this restriction there? Without this restriction, they
> can become much more useful and powerful.

From the concept anonymous function will never be a powerful tool. It's just a convenient tool.

>
> 3. Why are they called 'functions' then? Since in a normal matlab
> function, one can certainly do an assignment and use an if statement
> in them?

IMO FUNCTION is the right term, because it maps something to something else.

Bruno

TideMan

unread,
Jan 18, 2012, 5:44:25 AM1/18/12
to
On Jan 18, 8:43 pm, "Bruno Luong" <b.lu...@fogale.findmycountry>
wrote:
> "Nasser M. Abbasi" <n...@12000.org> wrote in message <jf57sl$qh...@speranza.aioe.org>...
Right on Bruno!!
My sentiments exactly.
Why use a tack hammer (anonymous functions) when you have a sledge
hammer (functions) in your tool bag?

Nasser M. Abbasi

unread,
Jan 18, 2012, 9:26:35 AM1/18/12
to
On 1/18/2012 4:44 AM, TideMan wrote:
> On Jan 18, 8:43 pm, "Bruno Luong"<b.lu...@fogale.findmycountry>
....
>> Bruno
>
> Right on Bruno!!
> My sentiments exactly.
> Why use a tack hammer (anonymous functions) when you have a sledge
> hammer (functions) in your tool bag?

Ok, you guys have convinced me.

No more anonymous functions for me ! But I'll miss arryfun() I think,
but I'll get over it with time.

thanks,
--Nasser

Steven_Lord

unread,
Jan 18, 2012, 10:07:08 AM1/18/12
to


"Nasser M. Abbasi" <n...@12000.org> wrote in message
news:jf57sl$qhb$1...@speranza.aioe.org...
>
> ref:
> http://www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html
>
> Matlab anonymous only supports making an `expression`, which makes
> them very restrictive. I can't do something as simple as this
> silly example:
>
> y=[0 0];
> arrayfun(@(i) y(i)=i ,1:2)
>
> and any other use of an anonymous function. Assignment to a variable is
> not allowed.
>
> (it is also not possible to do a logical test using an 'if' inside
> anonymous function)

That's correct. The body of an anonymous function must be ONE expression.
The first sentence of the page you linked says:

"Anonymous functions give you a quick means of creating *_simple_* functions
without having to store your function to a file each time." [Emphasis mine.]

> my question:
> 1. What is the recommended way to do assignments/updates to
> variables from inside an anonymous function? I can think of
> few, but was not sure if there is a know best method that
> most use.

If you're doing something like your ARRAYFUN example, you can't use
anonymous functions.

> 2. Why is this restriction there? Without this restriction, they
> can become much more useful and powerful.

% Assume we're in the base workspace
y = [0 0];
f = @(k) y(k) = k;
f(5)

If this were allowed, what should the variable y in the base workspace
contain after those three lines of code? Should the variable y in the base
workspace be [0 0 0 0 5]? Or should the copy of the variable y that was
captured in the anonymous function be changed to [0 0 0 0 5], leaving the
copy in the base workspace alone?

http://www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html#f4-71621

Using the fact that functions defined in files operate in their own
workspaces (ignoring nested functions for now, for sake of argument) as
precedent I'd have to say the latter. The base workspace's y is [0 0] and
the version in f's workspace is [0 0 0 0 5]. You could see that workspace
variable with the FUNCTIONS function.

That's not what you'd expect, is it?

Now to take this a bit further, what if I called f like:

z = f(3)

What should z be? Assignment in MATLAB doesn't return an output argument (as
in you can't do something like q = x = 2), and so this should (and would)
error.

> 3. Why are they called 'functions' then? Since in a normal matlab
> function, one can certainly do an assignment and use an if statement
> in them?

Because they _are_ functions [or specifically an anonymous function is a
function handle, just like @quad.] They accept input arguments, do
something, and return outputs. They don't have a file associated with them
like @quad does, but that's okay.

My mental model is that anonymous functions are not intended to supersede
regular function files. There are things regular functions can do that
anonymous functions can't. Instead, one of their main purposes is to be used
instead of inline objects when you need to do something simple. Rather than
having to write a function file:

function dydt = odefun(t, y)
dydt = t+y;

to solve a system of ODEs, you can define the system inline.

[t, y] = ode45(@(t, y) t+y, ...

They also provide an easy way to write the Adapter pattern without requiring
a change to either of the functions being "plugged into" the adapter.

http://www.mathworks.com/help/techdoc/math/bsgprpq-5.html

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Steven_Lord

unread,
Jan 18, 2012, 10:11:22 AM1/18/12
to


"TideMan" <mul...@gmail.com> wrote in message
news:edee8b99-f2c0-401c...@34g2000yqm.googlegroups.com...
> On Jan 18, 8:43 pm, "Bruno Luong" <b.lu...@fogale.findmycountry>
> wrote:
>> "Nasser M. Abbasi" <n...@12000.org> wrote in message
>> <jf57sl$qh...@speranza.aioe.org>...

*snip*

> Right on Bruno!!
> My sentiments exactly.
> Why use a tack hammer (anonymous functions) when you have a sledge
> hammer (functions) in your tool bag?

In that case, why have the tack hammer in your tool bag at all?

The answer is that there are situations where a tack hammer is appropriate
but you might get in serious trouble approaching it with a sledgehammer. For
instance, if you need to fix the seat on the antique chair you inherited
from Great Aunt Matilda by reattaching the cover to the frame, a bystander
may interpret your intentions a bit differently if you walked towards it
with sledge in hand. Particularly if you've said publicly that you never
really liked the chair ;)

Anonymous functions don't do everything. They're no Swiss Army Knife. [And
even (most) Swiss Army Knives don't do everything.] But they do have their
uses.

Christopher Crawford

unread,
Jul 30, 2017, 2:53:18 AM7/30/17
to
@Alec: brilliant! Your second version was exactly what I needed to plot a function with a few sliders to adjust parameters, for example to tune a function fitting. --thanks, Chris

x=[0:.1:10]'; a=.5; b=.5; f=@(a,b,x)sin(a*x+b); plt=@()evalin('caller','plot(x,f(a,b,x))'); plt();
ua=uicontrol('style','slider','position',[5,5,100,20],'Callback',@(es,ed)evalin('caller','a=ua.Value;plt()'));
ub=uicontrol('style','slider','position',[5,35,100,20],'Callback',@(es,ed)evalin('caller','b=ub.Value;plt()'));
0 new messages