function name as a variable

조회 수: 1 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by