mxGetScalar (C and Fortran)

Real component of first data element in array

C Syntax

#include "matrix.h"
double mxGetScalar(const mxArray *pm);

Fortran Syntax

real*8 mxGetScalar(pm)
mwPointer pm

Arguments

pm

Pointer to an mxArray; cannot be a cell mxArray, a structure mxArray, or an empty mxArray.

Returns

Pointer to the value of the first real (nonimaginary) element of the mxArray.

In C, mxGetScalar returns a double. If real elements in the mxArray are of a type other than double, mxGetScalar automatically converts the scalar value into a double. To preserve the original data representation of the scalar, cast the return value to the desired data type.

If pm points to a sparse mxArray, mxGetScalar returns the value of the first nonzero real element in the mxArray. If there are no nonzero elements, mxGetScalar returns 0.

Description

Call mxGetScalar to get the value of the first real (nonimaginary) element of the mxArray.

In most cases, you call mxGetScalar when pm points to an mxArray containing only one element (a scalar). However, pm can point to an mxArray containing many elements. If pm points to an mxArray containing multiple elements, mxGetScalar returns the value of the first real element. For example, if pm points to a two-dimensional mxArray, mxGetScalar returns the value of the (1,1) element. If pm points to a three-dimensional mxArray, mxGetScalar returns the value of the (1,1,1) element; and so on.

Use mxGetScalar on a 32-bit, nonempty mxArray of type numeric, logical, or char only. To test for these conditions, use MX Matrix Library functions such as mxIsEmpty, mxIsLogical, mxIsNumeric, or mxIsChar.

Examples

See the following examples in matlabroot/extern/examples/refbook.

See the following examples in matlabroot/extern/examples/mex.

See the following examples in matlabroot/extern/examples/mx.

See Also

mxGetM, mxGetN

Was this topic helpful?