–Loren

]]>I understand your point, I suppose I just haven’t had the need to use arrays/strings/etc in an anonymous function.

What do you think about modifying DISP to display the workspace contents captured in the anonymous function, using a display format similar to WHOS? I’m thinking something like this:

>> a = 1;

>> b = rand(10);

>> c = ‘test’;

>> fn = @(x) a*x.^2

fn =

@(x)a*x.^2

Captured variables:

Name Size Bytes Class Attributes

a 1×1 8 double

b 10×10 800 double

c 1×4 8 char

We intentionally do not show values in anonymous function displays. What if the variables to display had size 1000×1000; would you really want to see that? Doubtful. Technically, you can have a new function that you use to show that information, and possibly could overload display.

Please feel free to request this as an enhancement using the link to Technical Support on the right of my blog.

–Loren

]]>Do you think the DISP function could be modified to show the value of the captured variables, instead of the variable names? This would help people realize that they are indeed captured/frozen instead of taking on new ‘live’ values at the time of execution…

for instance:

>> a = 3.14;

>> fn = @(x) a*x.^2

fn =

@(x) 3.14*x.^2 % instead of current @(x) a*x.^2

>>

]]>I don’t understand your comment/code for the stand-alone cases you mention, especially @(x)@(y).

Your understanding of the meanings of the other calls is correct.

I am not clear what you want to see about multi-variable inputs. Can you say more?

–Loren

]]>I’ve to admit, though, I’d never understood its full potential, and after reading thru this nice article, more doubts haunt me.

For instance, in standalone cases, I haven’t see any difference while creating or calling anonymous functions with @(x) or @(x,y) or @(x)@(y).

As regards the above article, my understanding of the following lines of code is that:

myline = @(x) straightline(x,m,b);

is equivalent to myline(x);

and that

slopeIntercept = @(slope,intercept) straightline(data, slope, intercept);

is equivalent to slopeIntercept(slope,intercept), which in turn invokes straightline(…).

newline1 = @(X) straightline(X, slp(1), intcpt(1));

translates as newline1(X), which then calls straightline().

differentLine = @(slope, intercept, X) straightline(X, slope, intercept);

is the same as calling straightline() with a different order of parameters.

Please correct me if I’m wrong.

Could you shed further light on the issue, including multi-variable anonymous functions, & if possible, illustrate with examples where necessary?

Further, in case anyone’s used fmincon(), could you please share your experience and explain further? For instance, is it possible to optimize the function with multiple variables, in which case, you might need to use multi-variable anonymous functions as arguments to the objective function & the constraint function?

Thanks,

Navin.

The long-term plan is to have anonymous functions pick up only what they need from the environment. That is on the enhancement list for the language. For now, there is no way to avoid it – best to create the anonymous functions from a function if memory is an issue.

It would be helpful if you submitted the other wording and doc enhancements on mathworks.com. Thanks.

–Loren

]]>The current behavior can be unnecessarily memory/HD intensive, and in my experience has caught many programmers off guard… This is not helped by the fact that the documentation makes it sound like they only store referenced variables: http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html . Also, the wording of the following phrase could be improved, from “To supply different values for the coefficients, you must create a new function handle:” to “…you must redefine the function handle:”

Cheers

]]>I understand the desire for multi-statement anonymous functions. Right now, plot returns handles to the entities that are plotted, not to their axes.

If I understand your example, you would like that to change maybe (in addition to the multi-statements)?

–Loren

]]>I’d love to be able to write something like

doubleplot = @(x,a,b) …

subplot(1,2,1),varargout{1} = plot(x,a); …

subplot(1,2,2),varargout{2} = plot(x,b);

… and then be able to call

[ha hb] = doubleplot([1 2],[1 1],[2 1]);

… and see two subplots appear in the Figure and two handles to axes objects appear in the workspace.

]]>