Main Content


Convert duration or datetime array into numeric vector with the specified time unit


time2num is a function used in code generated by Diagnostic Feature Designer.

[x,origUnit] = time2num(T,targetUnit) converts the time array T from its original data type and unit into a numeric vector x expressed in the unit of targetUnit. For instance, suppose that T is a datetime vector that contains timestamps for data measurements, and you want to convert T into a numeric vector x that expresses the time in units of hours. Use x = time2num(T,"hours").

If x is already a numeric vector, time2num sets x to T and ignores targetUnit.

Code that is generated by Diagnostic Feature Designer uses time2num when performing spectral processing and other computations.

Input Arguments

collapse all

Time array of sampling instants, expressed as a one-dimensional datetime array, a one-dimensional duration array, or a numeric vector.

Time unit corresponding to the converted numeric vector, specified as a string. targetUnit can be one of the following:

  • "seconds"

  • "minutes"

  • "hours"

  • "days"

  • "years"

  • ""

If you omit targetUnit or set targetUnit to "", then time2num derives the original time unit from T.

  • If T is a duration array, then time2num sets timeUnit to the unit of the duration array.

  • If T is a datetime array, then time2num determines the best value for timeUnit based on sample time. For instance, if the timestamps in T are 100 seconds apart, time2num sets timeUnit to "minutes".

  • If T is a numeric array, then time2num ignores targetUnit and sets x to T.

Example: tNumeric = time2num(Tacho.Time,"seconds")

Output Arguments

collapse all

Sampling instants, returned as a numeric vector. The starting point x(1) depends on the data type of T.

  • If T is a datetime array, then x(1) is 0.

  • If T is a duration array or a numeric vector, then x(1) is T(1), converted to the unit in targetUnit if targetUnit is specified.

  • If T is a numeric vector, then x(1) is T(1).

Original unit of T, returned as a string.

Introduced in R2020a