passing function handle to separate function m-file error
Show older comments
Hi,
I have a function (function-1) which passes a function handle of the form:
fhandle = @fun; % step-1
to a function in a separate m-file (function-2). I invoke this function handle in function-2 using:
res = feval(fhandle, ...); % step-2
where the function in 'fhandle' is defined as a separate non-nested function within the function-2 m-file (function-2-1). However, this gives the following error:
"Undefined function 'fun' for input arguments of type 'double'."
I have also tried invoking the function handle directly, which also gives the same error:
res = fhandle(...); % step-2-v.2
This is odd, as if I go into function-2 in debug mode and re-enter the code in step-1 again, and then re-run step-2 and step-2-v.2, it seems to evaluate the function just fine. A work-around I have found is by defining the function handle as a string:
fhandle = 'fun'; % step-1-v.2
which seems to work fine. However, I have read that it is better practice to use the function handle form @, rather than passing a string. Could this be a bug or am I missing something?
Thanks
Accepted Answer
More Answers (0)
Categories
Find more on Desktop in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!