What is the type of function Handle and How is it stored in memory?

2 views (last 30 days)
I have been using function handle from a long time. Can someone explain how it is stored in memory. For example take the following code
M = [1, 2, 3];
FileAdd = @(M)(M+1);
This creates a Matrix M which is stored in a memory address in a sequencial manner with the order of its indices but how this FileAdd saved in the memory. The workspace looks something like this
Looking at the above figure I can say that M is stored a [1, 2, 3] where 1, 2 and 3 are stored in different memory location each having its own bit level representations. In the same way how can you represent FileAdd in bit level and how is it stored?. when i try
open(FileAdd);
It actually shows it has function handle. Somebody please explain.
Thank you.
If somebody has similar basic question on Matlab add it in comments I will add it in the same question, so that there is one place where otheres can access it
  6 Comments
Adam
Adam on 21 Mar 2019
Edited: Adam on 21 Mar 2019
doc functions
can be useful to see what is being stored with a function_handle as anonymous functions store a workspace with them which will add an overhead which may or may not be significant.
Simplest way is to try the alternatives and time them becaue even if you know how they are stored that still won't necessarily tell you what is most efficient without actually testing it out.

Sign in to comment.

Accepted Answer

Jan
Jan on 21 Mar 2019
Edited: Jan on 21 Mar 2019
M = [1, 2, 3];
FileAdd = @(M)(M+1);
This creates a "function handle" of an anonymous function, not a "file handle". The latter is simply a scalar double, which is created by fopen to access a file. A function handle is something completely different. Do not let the fact confuse you, that a "function" can be stored in a "file" also, because this does not mean, that they are equivalent. A "text" can be printed in a "book" also, but they are not the same.
"Looking at the above figure I can say that M is stored a [1, 2, 3] where 1, 2 and 3 are stored in different memory location each having its own bit level representations."
This sounds unclear. The code M=[1,2,3] creates a variable in the memory. In Matlab a variable is represented by a header of about 100 bytes, which contains beside other things a pointer to the memory, which contains the values, and the dimensions. The values are stored in the memory as array, here of type double, in a contiguous block.
How a function handle is stored, is not documented, because the user has no advantage from this knowledge. The internal storage migth be changed between Matlab releases, but this happens transparently and the exitsing code can be used without changes. We can guess, what the function handle must contain: In the given example, it must contain the code to evaluate x + 1 for any given input x. That the variable of the function handle is called M as the variable you have created before, is simply confusing. An example with different variables is more interesting:
M = [1, 2, 3];
FuncH = @(x) (x + M);
Now the contents of the variable "M" must be stored in the function handle also, because it is needed later on. See this:
M = [1, 2, 3];
FuncH = @(x) (x + M);
M = -1
FuncH(4)
You get the result [5, 6, 7], which is [1, 2, 3] + 4, while the new value of M=-1 does not matter.
This means, that a function handle stores the used variables also. We do not know, how they are stored.
The FileExchange submission https://www.mathworks.com/matlabcentral/fileexchange/45749-memory-efficient-anonymous-functions creates a function handle with the minimal amount of memory. A comment sounds, like in older Matlab versions, e.g. 2015a, the complete workspace at the creation of the function handle is copied, such that the memory consumption can be huge. In R2018a this problem seems to be fixed, but this submission helps to run some own experiments.
  4 Comments
Steven Lord
Steven Lord on 21 Mar 2019
This means, that a function handle stores the used variables also. We do not know, how they are stored.
That's correct, we don't say how the data is stored. I don't think that would be useful unless maybe you were trying to manipulate function handles inside a MEX-file, but I don't think we provide any documentation for how to do that.
If you need to see what data an anonymous function "remembers" for debugging purposes you could use the functions function. As my bold text and the Note on that documentation page states, don't use this as part of your function code but use it to help you in debugging your code.
Jan
Jan on 21 Mar 2019
"So can we say that function handles will not optimize the code" - I'm not sure what "optimizing the code" means here. A function handle is a handle of a function. It does not do anything actively. Function handles of anonymous functions contain a copy of the occurring parameters. This is their purpose and it is really useful, see e.g. Answers: Anonymous functions for provoding parameters .
Of course, if you modify the parameter, the anonymous function must be created again. This is safer and smarter than using global variables. It is hard to debug anonymous functions, because you cannot set a breakpoint inside them. Therefore I use them only for the above mentioned case to provide parameters for functions to be e.g. integrated, and in cellfun.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 21 Mar 2019
Firstly, you seem to be using the term file handle for what everybody else call function handles. File handles are completely different things and have absolutely nothing to do with functions. So please use the correct term to avoid confusing people.
Furthermore, your question seems to be more about anonymous functions rather than function handles. Anonymous functions are always used with function handles but function handles can also point to m file functions, local functions, nested functions, class methods, etc.
fhandle1 = @(x) x+1; %function handle pointing to an anonymous function
fhandle2 = @sin; %function handle pointing to a built-in function
The way matlab store function handles isn't documented and it is very likely that it will have no impact on the performance of your code anyway. You can assume that a function handle, regardless to what it points to will take a finite amount of storage. Probably small enough that it should be of no concern. As to the code it points to, regardless of its form (anonymous vs other type of function), by necessity it will use some memory. Matlab will not report the memory used by that code because it only reports memory used by data. Code (even anonymous functions) is not data. The amount of memory used by an anonymous function should be the same as the equivalent normal function (but JIT compilation may differ slightly)
In terms of performance, calling anonymous function is possibly slower than calling normal functions. This may very well change in future versions and unless you've demonstrated it's a problem by profiling your code, should be of no concern.
  2 Comments
Raghunandan V
Raghunandan V on 21 Mar 2019
Please check this answer here
Here I have created some function handles just for integration purpose. I finally made it work but the code looks ugly. Could you suggest a better way to use function handles here

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!