Cannot call function

8 次查看(过去 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