function name as a variable

5 ビュー (過去 30 日間)
feynman feynman
feynman feynman 2025 年 3 月 31 日
コメント済み: feynman feynman 2025 年 3 月 31 日
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 件のコメント
Stephen23
Stephen23 2025 年 3 月 31 日
No, but you could write a class and define FUN1 & FUN2 as its methods.
feynman feynman
feynman feynman 2025 年 3 月 31 日
also a good idea thanks

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2025 年 3 月 31 日
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 件のコメント
feynman feynman
feynman feynman 2025 年 3 月 31 日
thank you so much that works

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by