How to add datetime from seconds?

2 vues (au cours des 30 derniers jours)
hanif hamden
hanif hamden le 29 Mai 2019
Commenté : hanif hamden le 13 Juin 2019
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.

Réponse acceptée

Peter Perkins
Peter Perkins le 4 Juin 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 commentaire
hanif hamden
hanif hamden le 13 Juin 2019
Now I got it. Thank you very much sir!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by