Not able to use function handles in MATLAB 2006b version.
Mostrar comentarios más antiguos
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.
2 comentarios
Fangjun Jiang
el 12 de Oct. de 2011
Don't post duplicated questions.
Kishor
el 12 de Oct. de 2011
Respuestas (3)
Paulo Silva
el 29 de Jul. de 2011
0 votos
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 comentarios
Kishor
el 1 de Ag. de 2011
Walter Roberson
el 1 de Ag. de 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
el 1 de Ag. de 2011
@Kishor: and now please describe "it fails" with any details.
Kishor
el 12 de Oct. de 2011
Kishor
el 12 de Oct. de 2011
Walter Roberson
el 12 de Oct. de 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
el 12 de Oct. de 2011
Walter Roberson
el 12 de Oct. de 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.
1 comentario
Kishor
el 12 de Oct. de 2011
Fangjun Jiang
el 12 de Oct. de 2011
0 votos
The code runs without error if you fix:
missing global definition in function Handles()
duplicated function SUB_GetFuncHandle()
8 comentarios
Kishor
el 12 de Oct. de 2011
Fangjun Jiang
el 12 de Oct. de 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
el 12 de Oct. de 2011
Fangjun Jiang
el 12 de Oct. de 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
el 12 de Oct. de 2011
Fangjun Jiang
el 12 de Oct. de 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
el 12 de Oct. de 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
el 13 de Oct. de 2011
Categorías
Más información sobre Function Handles en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!