Execution time of functions used in anonymous function creation

3 views (last 30 days)
Hello,
What is the best way to force immediate execution of a function that is being used as part of an anoymous function? Do you have to evaluate it in a previous line or can you somehow evaluate it at the same time as defining ?
Here's a simple example:
rng(0)
randomNum = @() rand
defines a very different function than
rng(0)
currentRandom = rand
randomNum = @() currentRandom
Is there a way to get the behavior of the second example with only two lines?
Or here's another more meaningful example:
% Capture current stack
currentStack = dbstack
stackAnalyzer = @(x) stackAnalysisFunction(x,currentStack)
defines a very different function than the much cleaner looking:
% Capture current stack
stackAnalyzer = @(x) stackAnalysisFunction(x,dbstack)

Accepted Answer

Walter Roberson
Walter Roberson on 26 Oct 2020
Edited: Walter Roberson on 26 Oct 2020
What is the best way to force immediate execution of a function that is being used as part of an anoymous function?
stackAnalyer = str2func([@(x) stackAnalyzerFunction(x', struct2str(dbstack), ')'])
where struct2str() is a function that you would have to write that accepts an input struct and outputs a character vector that, when evaluated, builds a struct that had the same contents as the input struct
Thus stackAnalyzer would end up being something like
@(x) stackAnalyzerFunction(x, struct('file', {'/Users/roberson/MATLAB/626/626588/example_fun.m', '/Users/roberson/MATLAB/626/626588/driver.m'}, 'name', {'example_fun', 'driver'}, 'line', {3, 17}))
However, as there are some data structures that cannot be built from static text, in practice what you would have to end up doing instead is having struct2str() be a call to a serialization routine and the generated result would look something like
@(x) stackAnalyzerFunction(x, deserialize('1gSviPKojg1FuITTmVjoYBIgvfDDowCaw-E40IO3PFwEoyO2cJS05a56UVzWcnDp4.AZj.kfzkQGx0Pd-FAkISK8..auXZiGYi0REiXfBc1KnJiGsIJscBt7bPwMEJRle2-OCU9.NPu.7jaQeiCAgeDU37K8mlL74rDanIezyMn5z05-SnzPsAO'))
In view of the alternatives, are you sure this is wise?
To state things more explicitly:
NO, there is no way to have anything similar to @ evaluate an expression at the time the anonymous function is being built, and store the result in a variable attached to the anonymous function, and refer to the variable in the generated anonymous function.
.. Well, I wouldn't rule it out completely, but it is notoriously hard to inject values into an anonymous function. You can use functions() and access the workspace field and {1} that... but storing values into there is tricky.
  1 Comment
Matt
Matt on 26 Oct 2020
Thanks Walter,
I think that's a case of the solution being worse than the problem! I'll stick with the alternative.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!