Not able to use function handles in MATLAB 2006b version.
이전 댓글 표시
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.
답변 (3개)
Paulo Silva
2011년 7월 29일
0 개 추천
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
Kishor
2011년 8월 1일
Walter Roberson
2011년 8월 1일
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
2011년 8월 1일
@Kishor: and now please describe "it fails" with any details.
Kishor
2011년 10월 12일
Kishor
2011년 10월 12일
Walter Roberson
2011년 10월 12일
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
2011년 10월 12일
Walter Roberson
2011년 10월 12일
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
2011년 10월 12일
0 개 추천
The code runs without error if you fix:
missing global definition in function Handles()
duplicated function SUB_GetFuncHandle()
댓글 수: 8
Kishor
2011년 10월 12일
Fangjun Jiang
2011년 10월 12일
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
2011년 10월 12일
Fangjun Jiang
2011년 10월 12일
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
2011년 10월 12일
Fangjun Jiang
2011년 10월 12일
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
2011년 10월 12일
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
2011년 10월 13일
카테고리
도움말 센터 및 File Exchange에서 Function Handles에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!