Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# length

Length of vector or largest array dimension

## Syntax

numberOfElements = length(array)

## Description

numberOfElements = length(array) finds the number of elements along the largest dimension of an array. array is an array of any MATLAB® data type and any valid dimensions. numberOfElements is a whole number of the MATLAB double class.

For nonempty arrays, numberOfElements is equivalent to max(size(array)). For empty arrays, numberOfElements is zero.

## Examples

Create a 1-by-8 array X and use length to find the number of elements in the second (largest) dimension:

```X = [5, 3.4, 72, 28/4, 3.61, 17 94 89];

length(X)
ans =
8
```

Create a 4-dimensional array Y in which the third dimension is the largest. Use length to find the number of elements in that dimension:

```Y = rand(2, 5, 17, 13);

length(Y)
ans =
17```

Create a struct array S with character and numeric fields of different lengths. Use the structfun function to apply length to each field of S:

```S = struct('f1', 'Name:', 'f2', 'Charlie', ...
'f3', 'DOB:', 'f4', 1917)
S =
f1: 'Name:'
f2: 'Charlie'
f3: 'DOB:'
f4: 1917

structfun(@(field)length(field), S)
ans =
5
7
4
1```

## See Also

Was this topic helpful?