duration — Create MATLAB-compatible duration arrays from hour, minute, and second components.
duration constructs MATLAB-style duration objects from numeric hour, minute, and second components. Scalar component inputs expand to match non-scalar arrays, the resulting object stores elapsed time internally as days, and the writable Format property controls display, string, and char conversion.
How duration works in RunMat
duration(H)treats the input as hours.duration(H, M)treats the inputs as hours and minutes.duration(H, M, S)treats the inputs as hours, minutes, and seconds.- Scalar component inputs broadcast to match non-scalar component arrays; non-scalar inputs must share the same shape.
- The
Formatproperty controls text rendering. Supported values arehh:mm:ss,hh:mm,mm:ss,s, andss. - Linear indexing
d(k)is supported and returns duration values. - Comparison operators and
plus/minusduration arithmetic are supported. - Adding a duration to a datetime returns a datetime shifted by that elapsed time.
How duration runs on the GPU
duration does not allocate GPU objects or invoke provider kernels. If a numeric input originates on the GPU, RunMat gathers it before building the duration object.
GPU memory and residency
No. RunMat represents duration values as host-side objects with an internal day-count tensor and a Format property. Even when the constructor receives gathered numeric data, the resulting object remains resident on the CPU.
Examples
Constructing a scalar duration from numeric components
t = duration(1, 30, 45)Expected output:
t =
01:30:45Broadcasting scalar minutes across an hour vector
t = duration([1 2], 15)Expected output:
t =
01:15:00 02:15:00Changing the display format without changing the stored value
t = duration(1, 5, 0);
t.Format = 'hh:mm';
disp(t)Expected output:
01:05Adding a duration to a datetime
t0 = datetime(2024, 4, 9, 8, 0, 0);
t1 = t0 + duration(1, 30, 0)Expected output:
t1 =
09-Apr-2024 09:30:00FAQ
What does the Format property change?
Only the textual representation. The stored elapsed time is unchanged, so comparisons and arithmetic still refer to the same duration.
Can I index duration arrays?
Yes. Linear () indexing is supported and preserves the duration type.
Does duration run on the GPU?
No. duration values are represented as host-side objects. Numeric inputs may be gathered first, but the resulting duration object remains on the CPU.
Can I add a duration to a datetime?
Yes. RunMat supports datetime + duration and returns a datetime shifted by the specified elapsed time.
Related functions to explore
These functions work well alongside duration. Each page has runnable examples you can try in the browser.
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how duration works, line by line, in Rust.
- View duration.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.