User`s manual

Examples of C MEX-Files
2-25
/* The gataway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
const int dims[] = {2,2};
unsigned char *start_of_pr;
unsigned short data[] = {1,2,3,4};
int bytes_to_copy;
/* Call the computational subroutine. */
dbl_elem(data);
/* Create a 2-by-2 array of unsigned 16-bit integers. */
plhs[0] = mxCreateNumericArray(NDIMS,dims,
mxUINT16_CLASS,mxREAL);
/* Populate the real part of the created array. */
start_of_pr = (unsigned char *)mxGetPr(plhs[0]);
bytes_to_copy = TOTAL_ELEMENTS * mxGetElementSize(plhs[0]);
memcpy(start_of_pr,data,bytes_to_copy);
}
At the MATLAB prompt, entering
doubleelement
produces
ans =
2 6
8 4
The output of this function is a 2-by-2 matrix populated with unsigned 16-bit
integers. You can view the contents of this matrix in MATLAB, but you cannot
manipulate the data in any fashion.
Manipulating Multidimensional Numerical Arrays
Multidimensional numerical arrays were a new data type introduced in
MATLAB 5. For a discussion of the features of multidimensional numerical
arrays and the built-in functions MATLAB provides to manipulate them, refer
to “Multidimensional Arrays” in the Using MATLAB manual. Like all other