Example Mex Function

From University of Washington - Ubicomp Research Page
Jump to: navigation, search

Here is an example function which takes four arguments, and can be used to write sensor data in a text format. The overall operation of the function is fairly simple and shows how to use a number of the data manipulation functions of the Matlab API. The function takes four arguments:

  1. A filename to open
  2. A character name describing a sensor (i.e. "msb/accel_x")
  3. An input array
  4. A double indicating an endOfFrame

There are two basic modes of operation, one to write data from the inputArray out and another to write '==endOfFrame==' to the file instead of writing an input array out.

<cpp>#include "mex.h"

  1. include "matrix.h"
  2. include <stdio.h>


void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // mex_fprintf( outputFilename, sensorName, inputArray, endOfFrame ); // prhs[0] prhs[1] prhs[2] prhs[3] #define outputFilename_in (prhs[0]) #define sensorName_in (prhs[1]) #define inputArray_in (prhs[2]) #define endOfFrame_in (prhs[3])

double* dataWritten = NULL; char* outputFilename = NULL; char* sensorName = NULL; FILE* outputFile = NULL;

// If we have enough inputs to open a file, do so if( nrhs >= 1 ) { outputFilename = mxArrayToString( outputFilename_in ); outputFile = fopen( outputFilename, "at+" ); if( outputFile == NULL ) { printf( "ERROR: Unable to open file %s, exiting\n", outputFile ); mxFree(outputFilename); return; } mxFree(outputFilename); } else return;

if( nlhs >= 1 ) { plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL); dataWritten = mxGetPr(plhs[0]); *dataWritten = 0; }


// 4 arguments means we just want to write an end of file message if( nrhs == 4 ) { double* endOfFile; endOfFile = mxGetPr(endOfFrame_in); if( *endOfFile >= 1 ) { fprintf( outputFile, "==endOfFrame==\n" ); fclose( outputFile ); return; } }

// 3 arguments is the standard write: if( nrhs == 3 ) { // Get the input array and the size (we choose the longest dimension) mwSize noCols = mxGetN( inputArray_in ); mwSize noRows = mxGetM( inputArray_in ); mwSize noElements = noCols; if( noRows > noCols ) noElements = noRows;

if( noElements == 0 ) // Nothing to do return; else if( dataWritten != NULL ) *dataWritten = noElements;


// Write the sensor name sensorName = mxArrayToString( sensorName_in ); fprintf( outputFile, "%s ", sensorName );

// Write the data elements if( mxIsInt32( inputArray_in ) ) { //printf( "int32, %d\n", noElements ); __int32* inputArray = (__int32*)mxGetData( inputArray_in ); for( int cur=0; cur<noElements; cur++ ) fprintf( outputFile, " %d", *(inputArray+cur) ); } else if( mxIsDouble( inputArray_in ) ) { //printf( "double, %d\n", noElements ); double* inputArray = (double*)mxGetData( inputArray_in ); for( int cur=0; cur<noElements; cur++ ) fprintf( outputFile, " %.0Lf", *(inputArray+cur) ); } else if( mxIsSingle( inputArray_in ) ) { //printf( "single, %d\n", noElements ); float* inputArray = (float*)mxGetData( inputArray_in ); for( int cur=0; cur<noElements; cur++ ) fprintf( outputFile, " %.0f", *(inputArray+cur) ); } else { const char* dataTypeName = mxGetClassName( inputArray_in ); printf( "ERROR: Unknown input array data type, \"%s\"!\n", dataTypeName ); }

// All done, new line and close the file fprintf( outputFile, "\n" ); fclose( outputFile ); return; } fclose( outputFile ); return; }</cpp>