Not able to use function handles in MATLAB 2006b version.
Show older comments
At the time of debugging function handles are working properly but when run complete program it fails. I am not able to find out cause behind that. Please reply.
Thanks & Regards, Kishor Dongare.
Answers (3)
Paulo Silva
on 29 Jul 2011
0 votes
Not enough info, from MATLAB 2006b documentation I see that it supports function handles, please provide more details like the error that MATLAB gives and a sample of the code.
7 Comments
Kishor
on 1 Aug 2011
Walter Roberson
on 1 Aug 2011
You did not show the error you encounter.
What you show indicates that Addition1 is a function handle, but it also shows that Addition1 is not defined within the scope of Summing.
Jan
on 1 Aug 2011
@Kishor: and now please describe "it fails" with any details.
Kishor
on 12 Oct 2011
Kishor
on 12 Oct 2011
Walter Roberson
on 12 Oct 2011
Jan does not like to be sent email on such matters.
I do not have 2006b, but I am pretty good at analyzing errors *when I am told what the error is*. "it fails" is not enough description to go on.
Kishor
on 12 Oct 2011
Walter Roberson
on 12 Oct 2011
Do not use plain arrays to store function handles. Use cell arrays. Otherwise, your line
assignin('caller','level1_function',function_handle.handles(1));
invokes the function handle stored in function_handle.handles, passing it an argument of 1, and that is in a context where an output value is expected to satisfy the assignin(). If you were using cell arrays the line would be
assignin('caller','level1_function',function_handle.handles{1});
which would not invoke the function function handle.
Fangjun Jiang
on 12 Oct 2011
0 votes
The code runs without error if you fix:
missing global definition in function Handles()
duplicated function SUB_GetFuncHandle()
8 Comments
Kishor
on 12 Oct 2011
Fangjun Jiang
on 12 Oct 2011
Inside the function Handles(), I think you intent to use the variable function_handle as a global variable, but you didn't declare it as global.
You can't have two functions with the same name. You have to choose one and delete the other one.
Kishor
on 12 Oct 2011
Fangjun Jiang
on 12 Oct 2011
I understand your above code is in one main.m file so function SUB_GetFuncHandle is a sub-function. Still, you defined it twice. It might be a problem. I don't have MATLAB V7.1 or V7.3.
Kishor
on 12 Oct 2011
Fangjun Jiang
on 12 Oct 2011
I ran it in R14 (V7.1). There are warnings but no errors.
>> main
Warning: Non-scalar arrays of function handles will continue to work in R14,
but will be illegal in R15, to support parenthesis notation for invocation
of function handles. To prepare for R15, and to avoid this warning,
use cell arrays of function handles instead of arrays. For more information,
type 'help function_handle' and see the section at the end entitled
Note on Backward Compatibility.
> In main>SUB_GetFuncHandle at 18
In main at 9
Warning: Non-scalar arrays of function handles will continue to work in R14,
but will be illegal in R15, to support parenthesis notation for invocation
of function handles. To prepare for R15, and to avoid this warning,
use cell arrays of function handles instead of arrays. For more information,
type 'help function_handle' and see the section at the end entitled
Note on Backward Compatibility.
> In main>SUB_GetFuncHandle at 18
In main>level1_function at 45
In main at 12
ans =
Wats up??????
Walter Roberson
on 12 Oct 2011
You have an 'end' statement in your main() routine. The JIT is allowed, in such a case, to assume that no variables will be "poofed into existence". You violate that assumption, so MATLAB is allowed to just not know about the poofed variables, and is allowed to operate corruptly if it encounters such a variable.
Before assigning a variable in a function workspace via assignin() or evalin() or load(), you should initialize the variable. You can initialize to anything at present, but better for potential future optimizations would be if you were to assign something of the same class as it will eventually become.
Kishor
on 13 Oct 2011
Categories
Find more on Function Handles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!