This is happening because INLINE functions are objects and not actual functions. Thus, they do not have the same scope rules as functions. When the following is created :
h = inline('g(f(x))')
h =
Inline function:
h(x) = g(f(x))
The INLINE constructor recognizes that "x" is a variable, but it assumes that f and g are functions, while the user actually meant them to be variables f and g. When the constructor goes to evaluate h, it has the input x and looks for functions g and f but isn't able to find them (for simplicity we assume there are no functions f or g on the path ). The variables f and g exist, however they exist as INLINE objects in the MATLAB workspace but not in the function "h".
Thus users will have to indicate to MATLAB what f and g are. First, in h, declare that f and g are variables as shown in the following code:
h = inline('g(f(x))','x','f','g')
h =
Inline function:
h(x,f,g) = g(f(x))
Then to execute, pass values to h for f and g :
Please Note: This works as of MATLAB 6.0 (R12). However in MATLAB R11 there was a bug in "subsref" (the function that makes f(...) evaluate the same as feval(f,...)) for INLINE objects. This caused the last line to generate errors. To workaround this bug in MATLAB R11, you will need to do the following :
h = inline('feval(g,(feval(f,x)))','x','f','g')
h(3,f,g)
ans =
27
Best Answer