| MATLAB Application Program Interface Reference | Help Desk |
mxGetNumberOfDimensions
Get the number of dimensions
#include "matrix.h" int mxGetNumberOfDimensions(const mxArray *array_ptr);array_ptr The number of dimensions in the specified
mxArray. The returned value is always 2 or greater.
Use mxGetNumberOfDimensions to determine how many dimensions are in the specified array. To determine how many elements are in each dimension, call mxGetSize.
Consider a MEX-file named CountDim that calls mxGetNumberOfDimensions to determine how many dimensions are in the first input argument.
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray
*prhs[])
{
int number_of_dimensions;
int *dims;
int c;
/* Look at the number of dimensions in the input mxArray. */
number_of_dimensions = mxGetNumberOfDimensions(prhs[0]);
/* Create a dimensions array having the same number of dimensions
as the input mxArray. Arbitrarily make the length of each
dimension be 2. */
dims = mxCalloc(number_of_dimensions, sizeof(int));
for (c=0; c<number_of_dimensions; c++)
dims[c]=2;
/* Create an mxArray of signed 16-bit integers. */
mxCreateNumericArray(number_of_dimensions, dims,
mxINT16_CLASS, mxREAL);
...
}
In MATLAB, create a three-dimensional mxArray named td. Then pass td as an argument to CountDim
>> td = rand(6, 4, 2); >> CountDim(td)Since
td is a 3-dimensional mxArray, CountDim creates a 2-by-2-by-2 mxArray. If td had been a 4-dimensional mxArray, CountDim would have created a 2-by-2-by-2-by-2.
For an additional example, see mxGetNumberOfDimensions.c in the mx subdirectory of the examples directory.
mxSetM, mxSetN