| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
numberOfElements = length(array)
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.
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 =
10Create 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
![]() | legendre | length (Map) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |