Comments on: Thoughts about Anonymous Functions https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Fri, 28 Sep 2012 17:41:31 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Loren Shure https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33181 Fri, 28 Sep 2012 17:41:31 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33181 In reply to Eric.

Interesting suggestion, Eric. Please enter that as an enhancement request. You can use the Technical Support link on the right of my blog.

–Loren

]]>
By: Eric https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33180 Fri, 28 Sep 2012 17:15:33 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33180 Thanks Loren (sorry for the name mixup earlier, I had just been talking to someone named Lori :)

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

]]>
By: Loren Shure https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33173 Thu, 27 Sep 2012 12:05:48 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33173 Eric-

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

]]>
By: Eric https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33171 Wed, 26 Sep 2012 23:43:05 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33171 Thanks Lori, it will be very nice when anonymous functions only grab referenced variables.

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

>>

]]>
By: Loren Shure https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33170 Wed, 26 Sep 2012 15:08:15 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33170 Navin-

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

]]>
By: Navin https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33169 Wed, 26 Sep 2012 13:54:11 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33169 I’ve been using Anonymous functions for over a year now, primarily as arguments for the fmincon() function [used for constrained non-linear optimization].
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.

]]>
By: Loren Shure https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33165 Tue, 25 Sep 2012 20:10:11 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33165 Eric-

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

]]>
By: Eric https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33164 Tue, 25 Sep 2012 16:25:15 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33164 Loren, is there any possibility of changing the way that anonymous functions snapshot their environment, so that only variables that are explicitly reference in the anonymous function definition are stored in it? From my brief thinking, the only behavior change that this would introduce is if existing user code plays ‘games’ with eval() in the anonymous function to access the non-explicitly referenced variables – this would no longer work, but do you think many users do this? It’s not documented functionality…

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: https://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

]]>
By: Loren Shure https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33160 Mon, 24 Sep 2012 15:51:46 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33160 Carl –

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

]]>
By: Carl https://blogs.mathworks.com/loren/2012/08/29/thoughts-about-anonymous-functions/#comment-33159 Mon, 24 Sep 2012 00:02:12 +0000 https://blogs.mathworks.com/loren/?p=515#comment-33159 What I’m sure Drzick and Bogdan meant is to create multi-statement anonymous functions. It’s the one thing R has I wish MATLAB had.

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.

]]>