MATLAB Application Program Interface Reference | Help Desk |
mxGetElementSize
Get the number of bytes required to store each data element
#include "matrix.h" int mxGetElementSize(const mxArray *array_ptr);array_ptr The number of bytes required to store one element of the specified
mxArray
, if successful. Returns 0 on failure. The primary reason for failure is that array_ptr
points to an mxArray
having an unrecognized class. If array_ptr
points to a cell mxArray
or a structure mxArray
, then mxGetElementSize
returns the size of a pointer (not the size of all the elements in each cell or structure field).
Call mxGetElementSize
to determine the number of bytes in each data element of the mxArray
. For example, if the mxClassID
of an mxArray
is mxINT16_CLASS
, then the mxArray
stores each data element as a 16-bit (2 byte) signed integer. Thus, mxGetElementSize
returns 2.
mxGetElementSize
is particularly helpful when using a nonMATLAB routine to manipulate data elements. For example, memcpy
requires (for its third argument) the size of the elements you intend to copy.
Consider a MEX-file that calls memcpy
to make a copy of whatever kind of data gets passed to it. The third argument to memcpy
is the number of bytes to copy. In order to determine the number of bytes, you must first call mxGetElementSize
.
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { int bytes_per_element, total_elements; void *pr, *mycopy; /* Get the characteristics of the input mxArray. */ bytes_per_element = mxGetElementSize(prhs[0]); total_elements = mxGetM(prhs[0]) * mxGetN(prhs[0]); pr = mxGetPr(prhs[0]); /* Allocate enough heap to hold a copy of the real elements of the input mxArray. Then copy them. */ mycopy = mxCalloc(total_elements, bytes_per_element); memcpy(mycopy, pr, bytes_per_element * total_elements); ...For an additional example, see
mxGetElementSize.c
in the mx
subdirectory of the examples
directory.
mxGetM
, mxGetN