Function handle as function input?

조회 수: 5 (최근 30일)
Payjay
Payjay 2017년 5월 23일
편집: Stephen23 2017년 5월 24일
Hello there, I have a function handle, for example f = @(x) x.^2 and I want to use this as a function input, something like (just schematic):
function scalar = fct(f(x));
scalar = int(@(x) f(x).*x^2);
end
Is that possible? Greets and thanks!

채택된 답변

Rik
Rik 2017년 5월 23일
Inside such a wrapper, I highly doubt that. But in principle it should be possible to do what you want. I haven't tested this code, but I think this should work. (After a peek at the doc for int, I suspect this would error, but the principle should hold, after all, f is just a variable, even if it is a weird variable because it is an anonymous function)
function scalar=fct(f)
temp=@(x) f(x).*x.^2;
scalar=int(temp);
end
  댓글 수: 3
Rik
Rik 2017년 5월 24일
What exactly is the error you are getting here? Because as I understand it, int needs more inputs if the first input does not contain a symbolic variable. So is the problem occurring at the function call level, or inside the function fct?
Payjay
Payjay 2017년 5월 24일
I am sorry youre answer worked! Thanks! And that what I wrote down above even works too! I have thousands nested functions and I understood the errors wrong ;-)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Handles에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by