Since MATLAB v7 we can use the new anonymous functionality to create small functions like:
>> A = 7;
>> F = @(x) A*x;
to get a simple multiplication by 7:
But, if you change the A value, it is not updated on the F workspace:
>> A = 8;
This is sometimes undesirable, and we guess that by using
>> G = str2func(func2str(F));
we fix this problem but then we get a warning
??? Undefined function or method '@(x)A*x' for input
arguments of type 'double'.
So, one is forced to use the EVAL function. This FUNC2FUNC function uses EVAL and EVALIN to solve this problem, and the user can even specify the workspace to be used: 'base' or 'caller' or directly the function F workspace like:
>> clear A
>> F = func2func(F,'A',8);
Besides it can be used to transform any array, cell, structure of functions names or handles into "updated" function handles.
Carlos Adrian Vargas Aguilera (2022). func2func.m v1.0 (Jul 2009) (https://www.mathworks.com/matlabcentral/fileexchange/24876-func2func-m-v1-0-jul-2009), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!