Why do I get the error message " ??? Error using ==> inlineeval "?

3 Ansichten (letzte 30 Tage)
Why do I get the error message " ??? Error using ==> inlineeval " when trying to evaluate h(3) where h(x) is the INLINE function g(f(x)) ?
For example, when the INLINE function is defined as follows, g(f(3)) is evaluated in MATLAB but h(3) returns an error message.
f = inline('x.^2')
g = inline('3*x')
h = inline('g(f(x))')
h(3)
??? Error using ==> inlineeval
Error in inline expression ==> g(f(x))
??? Undefined function or variable 'f'.

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 27 Jun. 2009
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 :
h(3,f,g)
ans =
27
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

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by