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.

2 Comments

Don't post duplicated questions.
ok.Duplicate question is removed.

Sign in to comment.

Answers (3)

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

I created handle for my function Addition by name Addition1.
just try this example and see the error massege in 2006b and it on other matlab version.
Example :
my first function is Summing.
Dir : E:\Summing.m;
function [ output_args ] = Summing(inp1,inp2)
output_args = Addition1(inp1,inp2);
%% Addition1 is function handle :
functions(Addition1)
ans is
>>function: 'Addition'
type: 'simple'
file: 'E:\New Folder\Addition.m'
My second function is
Dir : E:\New Folder\Addition.m;
function [ output_args ] = Addition( ip1,ip2)
output_args = ip1 + ip2;
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.
@Kishor: and now please describe "it fails" with any details.
@ Jan :
Hello Jan should i send you the files.Please send me your email id.
@ JAN and Walter : Do you have MATLAB 2006b version??
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.
I had four function
1. Main
2. Handles
3. level1_function
4. level2_function
Function Main and Handles are on same directory.
Function level1_function and level2_function are in different-different directory.
1. Call main function.
definitions of all functions are:
%%%%%%%%%%
%%%%%%%%%% Main function :
function Main()
% Generate handles for function 'level1_function' and 'level2_function'.
Handles();
% Load function handle in current workspace for function
% 'level1_function'.
SUB_GetFuncHandle();
% Call function 'level1_function' using function handle.
level1_function();
end
%% Subfunction :
function SUB_GetFuncHandle()
global function_handle;
assignin('caller','level1_function',function_handle.handles(1));
end
%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% Handle function
function Handles()
% Change folder structure if neccessary for generating function handle
% Save present directory
Present_Dir = pwd;
% Change directory to function 'level1_function' dir and generate function handle.
cd('E:\Testing\Level1');
function_handle.handles(1) = @level1_function;
% Change directory to function 'level2_function' dir and generate function handle.
cd('E:\Testing\Level2');
function_handle.handles(2) = @level2_function;
% Chage to original dir
cd (Present_Dir);
%% Save handle struct as 'global' in base workspace
evalin('base','global function_handle');
assignin('base','function_handle',function_handle);
end
%%%%%%%%%%
%%%%%%%%%% Function :
function level1_function()
% Load function handle in current workspace for function
% 'level2_function'.
SUB_GetFuncHandle();
level2_function('Wats up??????');
end
%% Sub function :
function SUB_GetFuncHandle()
global function_handle;
assignin('caller','level2_function',function_handle.handles(2));
% Check STACK data for function : level2_function
% Function handle is present for function level2_function
% Two options 1. Step run till the end and
% 2. Press F5
% Check the result.
sprintf(['\n Check MATLAB workspace ''STACK'' data of function : ''level2_function''',...
'\n Function handle is present for function level2_function',...
'\n If then continue....\n Two options 1. Step run till the end or',....
'\n\t\t\t 2. Press F5',...
'\n Check the result.'])
keyboard
end
%%%%%%%%%
%%%%%%%%%Function :
function level2_function( input_args )
sprintf(input_args)
%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%
Error message if Main function called with no break point.
Same function work file in matlab 2010a.
??? Error using ==> level2_function
Too many output arguments.
Error in ==> level1_function at 7
level2_function('Wats up??????');
Error in ==> Main at 6
level1_function();

Sign in to comment.

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 Comment

Still problem not solved.
My question is why it works fine if I run script step by step and throws error when run the script without breakpoint.

Sign in to comment.

The code runs without error if you fix:
missing global definition in function Handles()
duplicated function SUB_GetFuncHandle()

8 Comments

You mean to say I have to declare variable function_handle as global in function Handles() and what about duplicated function SUB_GetFuncHandle.
Can you please elaborate in brief.I getting same error.
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.
ok.
But the scope of function SUB_GetFuncHandle is within the function.It will not affect other function.
Above Script run without any error in MATLAB version 7.5 without any error.
Same script run without error in step by step execution in MATLAB version 7.3 but if I execute complete script it throws error.
If you have matlab version 7.1 or 7.3 you can try the above example.
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.
Ok.
Above code just replica of my original code.
I have written code in 7.10 version of matlab.
It run without any error.
At time of checking backward compatibility of code I fount this bug.
The script are not working version before 7.3
One more thing I want tell you: even I cant believe
If I made change in function 'level1_function()',
replace call of function "level2_function('Wats up??????');"
by
"level2_function;
level2_function('Wats up??????');"
Script run without any error.
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??????
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.
@Fangjun : Thank for your efforts.
I don't know why it is showing error to me even in 7.1 version of MATLAB and run without error in step execution.
@Walter : I didn't get your first comment.Can you elaborate using example.
I have tried with initialization of variables in function workspace before assigning, it's working.
One more thing even if I haven't initialized variable and assigned function handle to function workspace
Now before using function handle, I made operation like
"functions(var);"
and then used the variable as function handle script run without error.

Sign in to comment.

Categories

Asked:

on 29 Jul 2011

Community Treasure Hunt

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

Start Hunting!