How to input a function into a function?

I wrote a matlab function fun1 and fun2
and when I included them as inputs in third function fun3 that I wrote, it gives an error - not enough input arguments.

답변 (1개)

michio
michio 2020년 2월 15일
편집: michio 2020년 2월 15일

0 개 추천

Have you tried using funtion handles? You can use them as inputs instead of directly using function names.

댓글 수: 3

michio
michio 2020년 2월 15일
편집: michio 2020년 2월 15일
The function handles are variables, so you can save them in a mat file if you need to but you can just define them before calling fun4. If you are running fun4 in a command window then
f1 = @fun1;
f2 = @fun2;
f3 = @fun3;
fun4(f1, f2, f3)
I suppose here that fun1 to fun3 are already defined.
Cantor Set
Cantor Set 2020년 2월 15일
II want to nest m-files functions into an m-file function and then run the it.
The idea of function handles didn't work :(
michio
michio 2020년 2월 15일
Hm, I apologize that I haven’t given you a suitable example that suita your case, but those functions (fun1-fun3 above) can be defined as separate m-files or as subfunctions, or as nested functions. There should be a way to make it work :)
Could you give me more detailed info? Such as sample code or.. anything that explains your situation.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

질문:

2020년 2월 15일

댓글:

2020년 2월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by