Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Please login or create an account
Change language to: Français - Português - 日本語 - Русский
Scilab Help >> MEX Library API > mexlib

mexlib

Mexlib provides a C MEX API for Scilab. This module is based on the MEX specification.

How to

To use the MEX API you first need to compile a C file. Here is a small example (call it example.c):

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
double *output;
double data[] = {1.0, 2.1, 3.0};

plhs[0] = mxCreateDoubleMatrix(1, 3, mxREAL);
output = mxGetPr(plhs[0]);

memcpy(output, data, 3*sizeof(double));
}

This function just returns a double array with 3 values. The important part is the MEX API is used; mex.h is included and mexFunction defined with the right calling convention and arguments.

To compile this file, you should use Scilab ilib_mex_build function.

--> ilib_mex_build('libmexexample',['mexgetdouble','example', 'cmex'], 'example.c',[],'Makelib','','','');

This creates a loader file. To load it into Scilab, you should run:

--> exec('loader.sce');

List of different mex function categories

Data Types

Data types and constants.

Create or Delete Array

Create array of a particular type, allocate and free memory.

Validate Data

Evaluate array data type.

Access Data

Reading or writing data to an array.

Convert Data Types

Convert strings and structure array to object array.

Examples

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
double *output;
double data[] = {1.0, 2.1, 3.0};

plhs[0] = mxCreateDoubleMatrix(1, 3, mxREAL);
output = mxGetPr(plhs[0]);

memcpy(output, data, 3*sizeof(double));
}

After that, just call your function, created with name mexgetdouble:

--> ilib_mex_build("libmex",["mexgetdouble","mexgetdouble.c","cmex"], "mexgetdouble.c",[]);
--> exec("loader.sce");
--> r = mexgetdouble()
r  =

1    2.1    3
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])

{
mexPrintf("\nThis is an example Mex function.\n");
int n = 0;
mxArray* pOut = NULL;
n = mxGetNumberOfDimensions(prhs[0]);

pOut = mxCreateDoubleScalar((double)n);
plhs[0] = pOut;

}

Assuming that the file name is mexexample.c, just call your function, created with the name mexexamp:

--> ilib_mex_build("libmex",["mexexamp","mexexample.c","cmex"], "mexexample.c",[]);
--> exec("loader.sce");
--> a = ones(1, 2, 3, 4);
--> mexexamp(a)
This is an example Mex function.
 ans  =
    
 4.

See also

History

VersionDescription
6.1 Improved MEX API coverage.
5.0 MEX API initial support.
Scilab Enterprises
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Tue Feb 25 08:49:22 CET 2020