#include "mex.h"
#include <math.h>
void timestwo(double y[], double x[])
{
y[0] = 2.0*x[0];
return;
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{ double *y;
double *x;
unsigned int m, n;
/* Check for proper number of arguments. */
if (nrhs !=1) {
mexErrMsgTxt("Only one input argument allowed."); }
else if (nlhs !=1) {
mexErrMsgTxt("Only one output argument allowed."); }
/* The input x must be a scalar, get the size of x. */
m = mxGetM(prhs[0]); /* rows */
n = mxGetN(prhs[0]); /* columns */
if (!mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0])
|| mxIsSparse(prhs[0]) || !mxIsDouble(prhs[0])
|| !(m ==1 && n ==1))
{
mexErrMsgTxt("Input x must be a scalar.");
}
/* Create a matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
/* Assign pointers to each input and output. */
y = mxGetPr(plhs[0]);
x = mxGetPr(prhs[0]);
/* Call the timestwo subroutine. */
timestwo(y, x);
}
Il est important de faire les vérifications de type car MATLAB n'a
pas la même représentation de variable que C. Vérifier la validité
des types réduit le risque d'erreur.