Receiving inputs from MATLAB:

/*** Begin receive.c ***/ /* This program receives an mxArray from MATLAB (automatically sent * through prhs[]), gets a C pointer to it and prints out the value. */ #include "mex.h" void mexFunction (int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]) { double *dummy; dummy = mxGetPr(prhs[0]); /* dummy is now a regular C pointer */ mexPrintf("### input is %g ###\n", dummy[0]); } /*** End receive.c ***/ Compiling and running the receive.c in MATLAB: » mex receive.c » mextest(234) ### input is 234 ### »

Sending outputs to MATLAB:

/*** Begin mexout.c ***/ /* This file creates an mxArray, assigns it to the output mxArray * pointer plhs[0]; then gets a C "double" pointer to plhs[0] and sets * it's value to 987. * The */ #include "mex.h" void mexFunction (int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]) { double *dummy; plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); dummy = mxGetPr(plhs[0]); dummy[0] = 987; } /*** End mexout.c ***/ Compiling and running mexout.c in MATLAB: » mex mexout.c » mexout ans = 987 »

Back to Table of Contents