How to add datetime from seconds?
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!