How to save a function handle to a .m file
Afficher commentaires plus anciens
If I create a function via inline, e.g.
myfunc = @(x,y,z) x.^2+y+z
How can I save this as a matlab file function? I can do this if I chose to make a symbolic function this way
syms x y z
myfunc(x,y,z) = x.^2 + y + z;
matlabFunction(myfunc,"File","myfunc")
But I would like to accomplish this without having to create a symbolic function.
Réponse acceptée
Plus de réponses (1)
myfunc = @(x,y,z) x.^2+y+z;
writelines("myfunc = " + func2str(myfunc), "myfunc.m")
%crosscheck
dbtype myfunc.m
2 commentaires
myfunc = @(x,y,z) x.^2+y+z;
namedfunc2file(myfunc)
%crosscheck
dbtype myfunc.m
function namedfunc2file(funhandle)
if nargin < 1; error('must provide named function handle'); end
funname = inputname(1);
assert(isa(funhandle, 'function_handle') && ~isempty(funname), 'must be function handle assigned to variable');
funs = func2str(funhandle);
writelines(funname + " = " + funs, funname + ".m");
end
This could be improved to test whether the function handle is anonymous or not using functions() . If it is not anonymous then the file field returned by functions() will be empty for built-in functions, and otherwise will indicate the file that contains the function definition. If functions() says the type is simple then either it is the handle to a built-in function (in which case file field will be empty) or else it is the handle to a function that has its own file. If functions() says the type is scopedfunction then the handle is to a function defined within the file for a script, or is defined after the initial function for a function file. If functions() says the type is "nested" then the handle is to a function defined inside another function.
The case of simple and non-empty file field would seem at first to be the easiest to deal with -- but remember the goal is to extract only the function so named, and there might be additional functions after it in the file (some of which might be essential for the function to operate)....
The other cases other than 'simple' with empty file would require locating the function definition inside the containing file.
Okay, getting back to anonynmous functions:
z = rand(1,3)
myfunc = @(x,y) x.^2+y+z;
S = func2str(myfunc)
Notice that the text contains the name z, not the value associated with z inside the anonymous function:
info = functions(myfunc)
info.workspace{1}.z
so if the idea is that the generated function file should replicate the behaviour of the anonymous function, then really you need to examine the workspace and have code that assigns constant values to those variables. But... keep in mind that captured variables might themselves be anonymous functions. and that the captured anonymous functions might be variables with the same name
g = 2;
g = @(x) g * ones(size(x))
g = @(x) g(x) .* (1-g(x));
info = functions(g)
inner_g = info.workspace{1}.g
inner_info = functions(inner_g)
inner_info.workspace{1}.g
so we cannot simply (reliably) recurse and generate functions with the same name of nested anonymous functions: we would have to generate new names for each nested anonymous function and rewrite the function body to use the name name... and hope it wasn't sensitive to exact variable names.
Catégories
En savoir plus sur Function Creation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!