Scilab Home Page | Wiki | Bug Tracker | Forge | Mailing List Archives | Scilab Online Help | File Exchange
ATOMS : Apifun details
Login with GitLab


Check input arguments in macros.
(2427 downloads for this version - 142203 downloads for all versions)
A more recent valid version with binaries for Scilab 5.2 exists: 0.3
Michael Baudin
Owner Organization
Michael BAUDIN
Creation Date
July 5, 2011
Source created on
Scilab 5.2.x
Binaries available on
Scilab 5.2.x:
Windows 64-bit Windows 32-bit Linux 64-bit Linux 32-bit macOS
Install command
--> atomsInstall("apifun")

The goal of this module is to provide a set to function to check input arguments
in macros.

In functions with a variable number of input arguments,
we often have the following source code, which checks that 
the number of input arguments provided by the user is 
consistent with the number of expected arguments :

if ( rhs < 2 | rhs > 5 ) then
  errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d
provided while from %d to %d are expected."), "myfunction",

Writing this code is error-prone and boring.
Moreover, this fragment of source code is duplicated in 
many functions, leading to small variations depending on the 
In particular, this makes the localization of the error messages 
difficult, because the message can be slightly different 
depending on the function.
It can also lead to unexpected errors, for example when the 
string containing the error message is wrongly formatted. 
In this case, instead of the expected error, the user gets a message telling 
that the argument of the msprintf function has a wrong format ; this 
is an information which can let the user confused about the source 
of the error.
Moreover, because writing this source code is boring, many existing functions 
are not sufficiently robust against wrong uses.

The goal of the apifun module is to simplify this task. 
For example, the apifun_checkrhs function checks that the number of 
input arguments provided by the user of the function corresponds 
to the number of expected arguments.
The following function takes 2/3 input arguments and 1 output arguments.

function y = myfunction ( varargin )
  [lhs, rhs] = argn()
  apifun_checkrhs ( "myfunction" , rhs , 2:3 )
  apifun_checklhs ( "myfunction" , lhs , 1 )
  x1 = varargin(1)
  x2 = varargin(2)
  if ( rhs >= 3 ) then
    x3 = varargin(3)
    x3 = 2
  y = x1 + x2 + x3

When called with 1 or 4 arguments, an error will be 

// Calling sequences which work
y = myfunction ( 1 , 2 )
y = myfunction ( 1 , 2 , 3 )
// Calling sequences which generate an error
y = myfunction ( 1 )
y = myfunction ( 1 , 2 , 3 , 4 )

The following session shows the kind of error message which is
produced by the apifun module, for example when the number of input argument is
The error message is clear about the function which generates the message (e.g.

-->y = myfunction ( 1 )
 !--error 10000 
myfunction: Unexpected number of input arguments : 1 provided while the number
of expected input arguments should be in the set [2 3].
at line     131 of function apifun_checkrhs called by :  
at line       3 of function myfunction called by :  
y = myfunction ( 1 )

The advantages of using the current module are the following.
 * Designing robust functions is much simpler which improves both the 
   quality and the robustness of functions.
 * The provided source code is factored into one single 
 * It is similar to what is provided to the developers of Scilab gateways,
   at the C level.

The following is a list of the function currently implemented in this module:

 * apifun_argindefault — Returns the value of an input argument.
 * apifun_checkcallable — Generates an error if the variable is not a callable
 * apifun_checkdims — Generates an error if the variable has not the required
 * apifun_checkflint — Generates an error if the variable is not a floating
point integer.
 * apifun_checkgreq — Check that the value is greater or equal than a
 * apifun_checklhs — Generates an error if the number of LHS is not in given
 * apifun_checkloweq — Checks that the value is lower or equal than a
 * apifun_checkoption — Generates an error if the value of an input argument
is not expected.
 * apifun_checkrange — Check that the value is in a given range.
 * apifun_checkrhs — Generates an error if the number of RHS is not in given
 * apifun_checkscalar — Generates an error if the variable is not a scalar.
 * apifun_checksquare — Generates an error if the variable is not a square
 * apifun_checktype — Generates an error if the given variable is not of
expected type.
 * apifun_checkveccol — Generates an error if the variable is not a column
 * apifun_checkvecrow — Generates an error if the variable is not a row
 * apifun_checkvector — Generates an error if the variable is not a vector.
 * apifun_expandvar — Expand variables so that they all have the same shape.

Files (2)
[92.33 kB]
Source code archive
Oups: apifun_checkflint was missing in the source tree.
[175.84 kB]
OS-independent binary for Scilab 5.2.x
Binary version
Automatically generated by the ATOMS compilation chain

News (0)
Comments (2)     Leave a comment 
Comment -- July 5, 2011, 04:01:30 PM    
Here is the changelog of version 0.2.

apifun (0.2)
    * Removed unnecessary commas.
	* Fixed bug #480 in the name of the library
	* Added warning if already loaded.
	* Added help cleaner.
	* Removed auto-generated demos.
	* Fixed bug #129: created apifun_argindefault
	* Created unit tests for checkvector
	* Created unit tests for checkveccol
	* Created unit tests for checkvecrow
	* Fixed bug #153
		* Update apifun_checkvector to make the nbval optionnal
		* Update apifun_checkveccol to make the nbval optionnal
		* Update apifun_checkvecrow to make the nbval optionnal
	* Created unit test for checkrange.
	* Created unit test for checkoption.
	* Created unit test for checkgreq.
	* Created unit test for checkloweq.
	* Created unit test for checksquare.
	* Fixed bug #232: The apifun_checkrange function did not manage NANs.
	* Fixed bug #327: The expandvar function has been added to apifun.
	* Fixed bug #142: Add a apifun_checkcallable function
	* Fixed bug #141: Added a apifun_checkflint function.
	* Fixed internal error messages, when type of input arguments 
	  given to apifun_* functions is wrong.
 -- Michael Baudin <>  July 2011
Comment -- July 7, 2011, 04:04:28 PM    
Packaging of v0.2 checked on Windows XP:

 ans  =
!apifun  0.2-2  allusers  SCI\contrib\apifun\0.2-2  I  !
Start Apifun
	Load macros
	Load help
 ans  =
!apifun  0.2-2  allusers  SCI\contrib\apifun\0.2-2  !
   TMPDIR = C:\Users\baudin\AppData\Local\Temp\SCI_TMP_6332_

   001/019 - [SCI\contrib\apifun\0.2-2] argindefault...............passed 
   002/019 - [SCI\contrib\apifun\0.2-2] checkcallable..............passed 
   003/019 - [SCI\contrib\apifun\0.2-2] checkdims..................passed 
   004/019 - [SCI\contrib\apifun\0.2-2] checkflint.................passed 
   005/019 - [SCI\contrib\apifun\0.2-2] checkgreq..................passed 
   006/019 - [SCI\contrib\apifun\0.2-2] checklhs...................passed 
   007/019 - [SCI\contrib\apifun\0.2-2] checkloweq.................passed 
   008/019 - [SCI\contrib\apifun\0.2-2] checkoption................passed 
   009/019 - [SCI\contrib\apifun\0.2-2] checkrange.................passed 
   010/019 - [SCI\contrib\apifun\0.2-2] checkrhs...................passed 
   011/019 - [SCI\contrib\apifun\0.2-2] checkscalar................passed 
   012/019 - [SCI\contrib\apifun\0.2-2] checksquare................passed 
   013/019 - [SCI\contrib\apifun\0.2-2] checktype..................passed 
   014/019 - [SCI\contrib\apifun\0.2-2] checkveccol................passed 
   015/019 - [SCI\contrib\apifun\0.2-2] checkvecrow................passed 
   016/019 - [SCI\contrib\apifun\0.2-2] checkvector................passed 
   017/019 - [SCI\contrib\apifun\0.2-2] complete...................passed 
   018/019 - [SCI\contrib\apifun\0.2-2] complete2..................passed 
   019/019 - [SCI\contrib\apifun\0.2-2] expandvar..................passed 
Leave a comment
You must register and log in before leaving a comment.
Login with GitLab
Email notifications
Send me email when this toolbox has changes, new files or a new release.
You must register and log in before setting up notifications.