duration

Create duration array from numeric values

The duration function creates an array that represents elapsed time in units of fixed length, such as hours, minutes, and seconds. You also can create elapsed times in terms of fixed-length (24-hour) days and fixed-length (365.2425-day long) years.

Syntax

  • D = duration(H,MI,S)
    example
  • D = duration(H,MI,S,MS)
  • D = duration(X)
  • D = duration(___,'Format',displayFormat)
    example

Description

example

D = duration(H,MI,S) creates a duration array from numeric arrays containing the number of hours, minutes, and seconds specified by H, MI, and S, respectively.

D = duration(H,MI,S,MS) creates a duration array from numeric arrays containing the number of hours, minutes, second, and milliseconds specified by H, MI, S, and MS, respectively.

D = duration(X) creates a column vector of durations from a numeric matrix.

example

D = duration(___,'Format',displayFormat) additionally specifies the format in which D displays. You can use this syntax with any of the arguments from the previous syntaxes.

Examples

collapse all

D = duration(1,30:33,0)
D = 

  1×4 duration array

   01:30:00   01:31:00   01:32:00   01:33:00

Create a duration array from a matrix and display the values in units of fractional hours.

X = magic(3);
D = duration(X,'Format','h')
D = 

  3×1 duration array

   8.0183 hr
   3.0853 hr
   4.1506 hr

Input Arguments

collapse all

Hour, minute, and second arrays specified as a scalar, vector, matrix or multidimensional array. These arrays must be the same size, or any one can be a scalar.

Example: 12,45,07.451

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Hour, minute, second, and millisecond arrays, each specified as a scalar, vector, matrix or multidimensional array. The arrays must be the same size, or any one can be a scalar.

Example: 12,45,30,35

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Input matrix, specified in the form [H,M,S].

Example: [1,30,0]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Display format of D, the duration array, specified as a character vector. The displayFormat value does not change the duration values in D, only their display.

To display a duration as a single number that includes a fractional part (for example, 1.234 hours), specify one of the following character vectors.

Value of displayFormatDescription
'y'Number of exact fixed-length years. A fixed-length year is equal to 365.2425 days.
'd'Number of exact fixed-length days. A fixed-length day is equal to 24 hours.
'h'Number of hours
'm'Number of minutes
's'Number of seconds

To display a duration in the form of a digital timer, specify one of the character vectors:

  • 'dd:hh:mm:ss'

  • 'hh:mm:ss'

  • 'mm:ss'

  • 'hh:mm'

In addition, you can display up to nine fractional second digits by appending up to nine S characters. For example, 'hh:mm:ss.SSS' displays the milliseconds of a duration value to three digits.

Data Types: char

Output Arguments

collapse all

Output duration, returned as a duration array.

You can change the display format of a duration by modifying the Format property. For example, to display the duration, D, as a number of minutes, type:

D.Format = 'm'

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

Introduced in R2014b

Was this topic helpful?