1 /* ==================================================================== */
2 /* Template toolbox_skeleton */
3 /* Example detail in "API_scilab getting started" help page */
4 /* This file is released under the 3-clause BSD license. See COPYING-BSD. */
5 /* ==================================================================== */
6 #include "api_scilab.h"
8 #include <localization.h>
10 /* ==================================================================== */
11 int sci_foo(char *fname, unsigned long fname_len)
13 // Error management variable
16 ////////// Variables declaration //////////
18 int *piAddressVarOne = NULL;
19 double *matrixOfDouble = NULL;
20 double *newMatrixOfDouble = NULL;
23 int *piAddressVarTwo = NULL;
24 int *matrixOfBoolean = NULL;
25 int *newMatrixOfBoolean = NULL;
29 ////////// Check the number of input and output arguments //////////
30 /* --> [c, d] = foo(a, b) */
31 /* check that we have only 2 input arguments */
32 /* check that we have only 2 output argument */
33 CheckInputArgument(pvApiCtx, 2, 2) ;
34 CheckOutputArgument(pvApiCtx, 2, 2) ;
37 ////////// Manage the first input argument (double) //////////
38 /* get Address of inputs */
39 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
42 printError(&sciErr, 0);
46 /* Check that the first input argument is a real matrix (and not complex) */
47 if ( !isDoubleType(pvApiCtx, piAddressVarOne) || isVarComplex(pvApiCtx, piAddressVarOne) )
49 Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), fname, 1);
54 sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &matrixOfDouble);
57 printError(&sciErr, 0);
61 ////////// Manage the second input argument (boolean) //////////
63 /* get Address of inputs */
64 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
67 printError(&sciErr, 0);
71 if ( !isBooleanType(pvApiCtx, piAddressVarTwo) )
73 Scierror(999, _("%s: Wrong type for input argument #%d: A boolean matrix expected.\n"), fname, 2);
78 sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2, &matrixOfBoolean);
81 printError(&sciErr, 0);
85 ////////// Check the consistency of the two input arguments //////////
87 if ((m1 != m2) | - (n1 != n2))
89 Scierror(999, _("%s: Wrong size for input arguments: Same size expected.\n"), fname, 1);
94 newMatrixOfDouble = (double*)malloc(sizeof(double) * m1 * n1);
95 ////////// Application code //////////
96 // Could be replaced by a call to a library
98 for (i = 0; i < m1 * n1; i++)
100 /* For each element of the matrix, multiply by 2 */
101 newMatrixOfDouble[i] = matrixOfDouble[i] * 2;
104 newMatrixOfBoolean = (int*)malloc(sizeof(double) * m2 * n2);
105 for (i = 0; i < m2 * n2; i++)
107 /* For each element of the matrix, invert the value */
108 newMatrixOfBoolean[i] = matrixOfBoolean[i] == TRUE ? FALSE : TRUE;
111 ////////// Create the output arguments //////////
113 /* Create the matrix as return of the function */
114 sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, newMatrixOfDouble);
117 printError(&sciErr, 0);
121 /* Create the matrix as return of the function */
122 sciErr = createMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 2, m2, n2, newMatrixOfBoolean);
125 printError(&sciErr, 0);
129 ////////// Return the output arguments to the Scilab engine //////////
131 AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
132 AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2;
134 ReturnArguments(pvApiCtx);
138 /* ==================================================================== */