How to add datetime from seconds?

1 回表示 (過去 30 日間)
hanif hamden
hanif hamden 2019 年 5 月 29 日
コメント済み: hanif hamden 2019 年 6 月 13 日
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.

採用された回答

Peter Perkins
Peter Perkins 2019 年 6 月 4 日
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 件のコメント
hanif hamden
hanif hamden 2019 年 6 月 13 日
Now I got it. Thank you very much sir!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by