필터 지우기
필터 지우기

Cannot call function

조회 수: 3 (최근 30일)
Daniel
Daniel 2012년 3월 15일
Simple Problem: Cannot call function; Function function? Need to use function handles? Error: "Undefined function or method ... for input arguments of type 'double'." What am I missing?
Very simple code, I think this effectively teaches the problem to avoid -- once we can figure out what the problem is! Please help me; I'm so terribly frustrated. Here is the code:
a = [-1 0 1 2 3];
b = 2*a;
myinter = inline( 'interp1(xdata,ydata,vari)','xdata','ydata','vari')
myfun = inline('3*myinter(xdata,ydata,vari)','xdata','ydata','vari')
myinter(a,b,2.7) % testing to see that it works
myfun(a,b,3.3) % testing to see that it works
Here is the resulting output:
myinter =
Inline function:
myinter(xdata,ydata,vari) = interp1(xdata,ydata,vari)
myfun =
Inline function:
myfun(xdata,ydata,vari) = 3*myinter(xdata,ydata,vari)
ans =
5.4000
??? Error using ==> inlineeval at 15
Error in inline expression ==> 3*myinter(xdata,ydata,vari)
Undefined function or method 'myinter' for input arguments of type 'double'.
Error in ==> inline.subsref at 27
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr,
INLINE_OBJ_.expr);
Error in ==> script at 15
myfun(a,b,3.3) % testing to see that it works
I think it's because I'm trying to create a 'function function'? I don't understand why it's giving me this issue, though, since it seems no different from inline('2*sin(x)'), except sin is a built-in function, while mine are not. I'm trying to start simple: myfun will eventually be the integrand for the 'quad' command, with factors like x*exp(-y*z) where x, y, and z are all interpolated values. I must learn how to call functions within functions -- are such functions called "function functions" as http://www.mathworks.com/help/techdoc/ref/function_handle.html suggests?
I've spent several hours and I still don't understand function handles and when they must be used. Something about the syntax escapes me. Please help! Thank you.

채택된 답변

Walter Roberson
Walter Roberson 2012년 3월 15일
Inline functions are not evaluated in your current workspace. "myinter" is defined in only in your current workspace, and so is not available to "myfun"
Try
myinter = @(xdata, ydata, vari) interp1(xdata,ydata,vari);
myfun = @(xdata, ydata, vari) 3 * myinter(xdata, ydata, vari);
Note: in this situation you could abbreviate
myinter = @interp1;

추가 답변 (1개)

per isakson
per isakson 2012년 3월 15일
I never use inline, because I believe that anonymous functions are a better alternative
a = [-1 0 1 2 3];
b = 2*a;
myinter = @(xdata,ydata,vari) interp1( xdata, ydata, vari );
myfun = @(xdata,ydata,vari) 3 * myinter( xdata, ydata, vari );
myinter(a,b,2.7) % testing to see that it works
myfun(a,b,3.3) % testing to see that it works

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by