Create function handle from function file

2 visualizaciones (últimos 30 días)
Arthur Roué
Arthur Roué el 10 de Mzo. de 2020
Comentada: tommsch el 30 de Nov. de 2020
Is it possible to create a function handle from function file ? In other words, create a function_handle object @foo from file foo.m.

Respuesta aceptada

Star Strider
Star Strider el 10 de Mzo. de 2020
Editada: Steven Lord el 10 de Mzo. de 2020
Yes!
In some instances, that is necessary in order to evaluate the function.
Example —
function y = f(x)
y = x.^2 - 2;
end
fz = fzero(@f,1)
produces:
fz =
1.4142
[SL: typo fix]
  8 comentarios
Arthur Roué
Arthur Roué el 11 de Mzo. de 2020
Thanks for your answer Steven Lord, it's a nice way to call a function not in scope.
I do use the testsuite call to run my tests. The reason why I run it in a separate MATLAB session is because I also loop on MATLAB versions to ensure the compatibility ot the toolbox I created in several MATLAB versions.
tommsch
tommsch el 30 de Nov. de 2020
@Steven Lord
The function for which you want to create the function handle must be in scope when the function handle is created. It does not need to be in scope when the function handle is evaluated.
When the function is shadowed by another function later on, the function handle may point to another function.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by