MATLAB Function Reference |

Number of elements in array or subscripted array expression

**Syntax**

**Description**

```
n = numel(A)
```

returns the the number of elements, `n`

, in array `A`

.

```
n = numel(A,varargin)
```

returns the number of subscripted elements, `n`

, in `A(index1,index2,...,indexn)`

, where `varargin`

is a cell array whose elements are `index1`

, `index2`

, `...`

, `indexn`

.

MATLAB implicitly calls the `numel`

builtin function whenever an expression such as `A{index1,index2,...,indexN}`

or `A.fieldname`

generates a comma-separated list.

`numel`

works with the overloaded `subsref`

and `subsasgn`

functions. It computes the number of expected outputs (`nargout`

) returned from `subsref`

. It also computes the number of expected inputs (`nargin`

) to be assigned using `subsasgn`

. The `nargin`

value for the overloaded `subsasgn`

function consists of the variable being assigned to, the structure array of subscripts, and the value returned by `numel`

.

As a class designer, you must ensure that the value of `n`

returned by the builtin `numel`

function is consistent with the class design for that object. If `n`

is different from either the `nargout`

for the overloaded `subsref`

function or the `nargin`

for the overloaded `subsasgn`

function, then you need to overload `numel`

to return a value of `n`

that is consistent with the class' `subsref`

and `subsasgn`

functions. Otherwise, MATLAB produces errors when calling these functions.

**Examples**

Create a 4-by-4-by-2 matrix. `numel`

counts 32 elments in the matrix.

a = magic(4); a(:,:,2) = a' a(:,:,1) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 a(:,:,2) = 16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1 numel(a) ans = 32

**See Also**

`nargin`

, `nargout`

, `prod`

, `size`

, `subsasgn`

, `subsref`

num2str | nzmax |