Adding number of seconds from changing base date

조회 수: 6 (최근 30일)
Eric Beamesderfer
Eric Beamesderfer 2020년 8월 13일
댓글: Eric Beamesderfer 2020년 8월 13일
This ones been giving me some trouble. I'm calling a file where the 'time' variable is the seconds since midnight of the day of interest which constantly changes:
In this test example, I am attempting to call four days of data at one time in a for loop. 's' is just the changing file name, but here when I attempt to take the 'time_raw' from the time variable for each file, it gives me the seconds since midnight of the current day, not the days of interest. I attempted to make some datetime values (t) of the days of interest and thought to concatenate them together (day of interest plus datetime of seconds since midnight) but I am unable to at this time. Feel like I'm missing one line of code to fix this, just not sure what that would be.
Currently, my 't2' variable is the correct number of seconds found in 'time' but for August 12, 2020 not April 16-19, 2010.

채택된 답변

Steven Lord
Steven Lord 2020년 8월 13일
You can construct a datetime directly from the number of seconds. From the documentation page for datetime:
"t = datetime(X,'ConvertFrom',dateType) converts the numeric values in X to a datetime array t. The dateType argument specifies the type of values in X."
One of the dateType options is 'epochtime', "Number of seconds since an epoch." If you use this "You must also specify epochValue, which is a scalar datetime, or a character vector or string scalar representing the epoch time."
>> datetime(100, 'ConvertFrom', 'epochtime', 'Epoch', datetime(2010, 4, 16))
ans =
datetime
16-Apr-2010 00:01:40
  댓글 수: 2
Eric Beamesderfer
Eric Beamesderfer 2020년 8월 13일
Thanks, Steven. This and the other answer are very close to working for me. However, I should have mentioned the number of seconds resets with each file/day. So on the next day, its back to close to zero. Currently, this solution restarts the number of seconds but doesn't update the correct day.
Eric Beamesderfer
Eric Beamesderfer 2020년 8월 13일
Slightly modifying your answer to add a changing date worked. Thanks again!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mohammad Sami
Mohammad Sami 2020년 8월 13일
Since the time is given to you as seconds since some date. you can read it as a double and then add it to the date of interests.
dateofinterest = datetime(2010,04,16);
% read the seconds
s = 1:100; % replace with your code to read the value.
out = dateofinterest + seconds(s);
  댓글 수: 1
Eric Beamesderfer
Eric Beamesderfer 2020년 8월 13일
Thank you, Mohammad. This didn't work at first, as the time (number of seconds since midnight) restarts with each day/file. Initially, your solution created a time array where after midnight, the time restarted, but it was still for the first day (April 16th) not the second day (April 17th). I slightly modified your answer and then it worked.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by