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