MATLAB Answers

How to add datetime from seconds?

13 views (last 30 days)
I have txt file as attached.
I have initial date time on the first row.. How i want to add this datetime with seconds for each row from first column?
This is example of my coding:
%% Input data
A = dlmread('ex1.txt');
% Identify year, month, day, hour, minute, second [Y,M,D,H,Mn,S]
Y = A(1,1); M = A(1,2); D = A(1,3); H = A(1,4); Mn = A(1,5); S = A(1,6);
dt = datetime(Y,M,D,H,Mn,S)
% Identify parameter after 2nd Row
Ts = seconds(A(2:end,1)); lat = A(2:end,2); lon = A(2:end,3); h = A(2:end,4);
dt.Second = dt.Second + Ts
When I run, the error showed like this:
Error using Test (line 12)
Numeric input data must be real.


Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 4 Jun 2019
That's not the error that I get, but it's possible that you are using an older version with different error handling.
In any case: The .Seconds property of a datetime, like all six of the time component properties, is a raw numeric value (it's the .Second property after all, there'd be no point in it having units). You are trying to add a duration to it. You want to either do this:
dt.Second = dt.Second + A(2:end,1)
or (better) this:
dt = dt + seconds(A(2:end,1))

  1 Comment

hanif hamden
hanif hamden on 13 Jun 2019
Now I got it. Thank you very much sir!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by