Generates an error if the variable is not a callable function.
errmsg=apifun_checkcallable(funname,var,varname,ivar)
a 1-by-1 matrix of strings, the name of the calling function.
a matrix of valid Scilab data type, the variable
a 1-by-1 matrix of string, the name of the variable
a 1-by-1 matrix of floating point integers, the index of the input argument in the calling sequence
a 1-by-1 matrix of strings, the error message. If there was no error, the error message is the empty matrix.
Checks that the variable var is a function (type 11 or 13), a compiled function (type 130) or a list. If the variable var is a list, then the first element of the list must be a function.
This function is designed to be used in algorithms which require a callback (i.e. a callable function) as input argument. This situation typically happens in optimization or integration algorithms, where the algorithm must call back the function in order to evaluate at some point x. In this case, it often happens that the objective function requires extra-arguments. This is why a "callable" can also be a list, where the first element in the list is the function and the remaining elements are extra-arguments, which are appended at the end of the calling sequence.
function y=myf(varargin) // y=myf(x) // y=myf(x,a) [lhsnb,rhsnb]=argn() x = varargin(1) if ( rhsnb==1) then a = 2 else a = varargin(2) end y = a*x endfunction function y=myalgorithm(f, x) apifun_checkcallable ( "myalgorithm",f,"f",1 ) if ( typeof(f)=="function" ) then __f__ = f __args__ = list() else __f__ = f(1) __args__ = f(2:$) end y = __f__(x,__args__(:)) endfunction // Calling sequences which work y = myalgorithm ( myf,2 ); y = myalgorithm ( list(myf,3),2 ); // Calling sequences which generate an error y = myalgorithm ( 1,2 ) y = myalgorithm ( list(3,4),2 ) | ![]() | ![]() |