function name as a variable

1 vue (au cours des 30 derniers jours)
feynman feynman
feynman feynman le 31 Mar 2025
Commenté : feynman feynman le 31 Mar 2025
Is it possible to do the following?
variablefun.m
function variablefun(fun)
fun
function fun1
end
function fun2
end
end
command window
variablefun(fun1)
variablefun(fun2)
  2 commentaires
Stephen23
Stephen23 le 31 Mar 2025
No, but you could write a class and define FUN1 & FUN2 as its methods.
feynman feynman
feynman feynman le 31 Mar 2025
also a good idea thanks

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 31 Mar 2025
variablefun('fun2')
fun2 here
function variablefun(fun)
eval(fun)
function fun1
disp('fun1 here')
end
function fun2
disp('fun2 here')
end
end
However, you cannot use
writefun(fun2)
as fun2 will not be defined at that point. MATLAB always works by evaluating parameters first before calling the given function, so in MATLAB it is not possible at all to define special "unevaluated parameter" behaviour (such as Maple is able to do.)
  1 commentaire
feynman feynman
feynman feynman le 31 Mar 2025
thank you so much that works

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by