What is the type of function Handle and How is it stored in memory?
Show older comments
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
madhan ravi
on 21 Mar 2019
https://in.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html - perhaps you mean this?
Raghunandan V
on 21 Mar 2019
Rik
on 21 Mar 2019
Why do you want to know this?
It's probably something Mathworks will not happily provide details on, and it might change between releases. You could get a memory dump and look through the memory yourself though.
Raghunandan V
on 21 Mar 2019
Edited: Raghunandan V
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.
Raghunandan V
on 21 Mar 2019
Accepted Answer
More Answers (1)
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.
Categories
Find more on Whos 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!