#include "mex.h" /* Remember to include! */ /* When called from MATLAB, execution starts here. LeftHandSide = mexme(RightHandSide) nlhs = Number of arguments on Left-Hand Side plhs = Pointer to arguments on Left-Hand Side nrhs = Number of arguments on Right-Hand Side prhs = Pointer to arguments on Right-Hand Side */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Just print text. May be useful for debugging. */ mexPrintf("Hello, world!\n"); /* If we don't have exactly 1 input and 2 output */ if (nrhs!=1 || nlhs!=2) { /* Throw Error message */ mexErrMsgTxt("The number of input arguments must be 1. The number of output arguments must be 2.\n"); } /* Let's extract the size of input data */ const mwSize *dims; dims = mxGetDimensions(prhs[0]); /* Height */ int h = dims[0]; /* Width */ int w = dims[1]; mexPrintf("W: %i\nH: %i\n", w, h); /* Let's get the pointer to the input data (the numbers are converted to double) */ double *data = mxGetPr(prhs[0]); /* Let this function output the minimum and maximum values of input data and corresponding indices. It may not be clever to use mex-file to achieve this goal, because there are "min" and "max" functions in MATLAB. But this is just an example. :) */ /* Let's create some output variables */ /* First one is a 1x2 matrix of doubles (mxREAL) which stores the min and max values Here 1 is the number of rows, and 2 is the number of columns. */ mxArray *mxValues = mxCreateDoubleMatrix(1, 2, mxREAL); /* The pointer to the actual values in this matrix */ double *values = mxGetPr(mxValues); /* Second one is 2x2 matrix of integers(mxINT32_CLASS) which stores indices of min and max values. The last argument of mxCreateNumericArray is a flag to deal with complex numbers (ignore it now). */ mxArray *mxIndices = mxCreateNumericMatrix(2, 2, mxINT32_CLASS, mxREAL); /* Pointer to the actual values in this matrix */ int *indices = (int *)mxGetPr(mxIndices); /* Initialize the minimum and maximum using the first entry of the 'data' */ values[0] = data[0]; values[1] = data[1]; indices[0] = 1; indices[1] = 1; indices[2] = 1; indices[3] = 1; /* So let's loop over all numbers and store the minimum and maximum values. */ int x,y; for (y=0; yvalues[1]) { values[1] = curval; indices[2] = y+1; indices[3] = x+1; } } /* Give matlab results */ plhs[0] = mxValues; plhs[1] = mxIndices; }