I have learned recently that anonymous functions can carry around large amounts of unused data, even if this data is created after the anonymous function is instantiated. The following example, together with the FUNCTIONS comand, illustrates this,
function fun=test %place in an mfile
a=1;
b=2;
c=3;
fun=@(x)x+b+a;
a=7;
b=rand(1000);
c=5;
q=3;
r=4;
end
Now, back in the base workspace, when I apply the functions() command to 'fun', I see
>> fun=test; s=functions(fun); s.workspace{:}
ans =
b: 2
a: 1
ans =
fun: @(x)x+b+a
a: 1
b: [1000x1000 double]
c: 3
I would like to understand (with official documentation if possible) what rules anonymous functions use to decide what data to carry around. The above seems to suggest that s.workspace{1}
will always contain the external variables and their values that the anonymous function actually uses. Meanwhile s.workspace{2}
seems to contain updates to variables that came into scope before fun
was defined. Am I correct that these are the rules? But if so, then why, in the above, does s.workspace{2}
contain an update to b, but not to a and c?
These things seem important to understand, since obviously, I can end up carrying large amounts of unintended memory that was allocated after my anonymous function was created.
An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.
An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. 3. This function is useful for all scenarios. An anonymous function can be useful for creating IIFE(Immediately Invoked Function Expression).
An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation. Normal function definition: function hello() { alert('Hello world'); } hello();
Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class.
Your understanding of the anonymous function is totally correct. The question is, why a
and c
are not updated and q
and r
not shown. Matlab does some code analyse and removes every irrelevant line. Neither the variable is read nor a function is called. Compare the behavior with this code:
function fun=test %place in an mfile
a=1;
b=2;
c=3;
fun=@(x)x+b+a;
a=7;
b=rand(1000);
c=5;
q=3;
r=4;
eval('');
end
Because of the eval('')
the code analyse can't identify any irrelevant lines. The output is:
ans =
b: 2
a: 1
ans =
fun: @(x)x+b+a
a: 7
b: [1000x1000 double]
c: 5
q: 3
r: 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With