| MATLAB Application Program Interface Reference | Help Desk |
mxGetN
Get the total number of columns in a two-dimensionalmxArray or the total number of elements in dimensions 2 through N for an m-by-n array.
#include "matrix.h" int mxGetN(const mxArray *array_ptr);array_ptr The number of columns in the
mxArray.
Call mxGetN to determine the number of columns in the specified mxArray.
If array_ptr is an N-dimensional mxArray, mxGetN is the product of dimensions 2 through N. For example, if array_ptr points to a four-dimensional mxArray having dimensions 13-by-5-by-4-by-6, then mxGetN returns the value 120 (5x4x6). If the specified mxArray has more than two dimensions and you need to know exactly how many elements are in each dimension, then call mxGetSize.
If array_ptr points to a sparse mxArray, mxGetN still returns the number of columns, not the number of occupied columns.
Find the total number of elements in an mxArray:
#include "mex.h"
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray
*prhs[])
{
int rows, cols, total_elements;
rows = mxGetM(prhs[0]);
cols = mxGetN(prhs[0]);
total_elements = rows * cols;
printf("This matrix has %d elements.\n", total_elements);
}
For an additional example, see mxGetN.c in the mx subdirectory of the examples directory.
mxGetM, mxGetNumberOfDimensions, mxSetM, mxSetN