Timedata 'dd-mmm-yyyy HH:MM:SS' transform into seconds
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello,
I got a excel data in xlsx or csv, which looks like in the picture. ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/202228/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/202228/image.png)
The time vector should be transformed into seconds, so i wrote this code.
Matlab throws me an error. How can i transform it into seconds?
Thank you for Help!
NTtemp=xlsread('Data.xlsx');
out=datestr(NTtemp(StartZeitInZeile:end,1),'dd-mmm-yyyy HH:MM:SS');
NTneueZeit=[datenum( out, 'dd-mmm-yyyy HH:MM:SS' ) .* (24*60*60) - datenum( out(1,:), 'dd-mmm-yyyy HH:MM:SS' ) .* (24*60*60)];
NT(:,1)=NTneueZeit;
NT(:,2)=NTtemp(StartZeitInZeile:end,2);
댓글 수: 0
답변 (1개)
Steven Lord
2019년 1월 31일
I would avoid going through datenum and datestr. Instead I would import that column of data as a datetime array (which I believe readtable will do for you automatically.) If you then need the number of seconds since a particular time as an additional variable in that table, that's fairly straightforward. I'm going to compute how many seconds have elapsed since midnight.
rightnow = datetime('now')
lastMidnight = dateshift(rightnow, 'start', 'day')
elapsedSeconds = seconds(rightnow-lastMidnight)
As I type this it's about 10:30 in the morning, so elapsedSeconds should be about 37800 (10.5*3600). Your elapsedSeconds may be different depending on your time zone.
댓글 수: 1
Peter Perkins
2019년 1월 31일
Better yet ...
elapsedTime = rightnow-lastMidnight
That leaves elapsedTime as a duration, and you no longer need to worry about units.
참고 항목
카테고리
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!