importdata not reading time stamp from excel sheet accurately

4 ビュー (過去 30 日間)
Danya Pradeep Kumar
Danya Pradeep Kumar 2019 年 9 月 28 日
コメント済み: Ajay Kumar 2019 年 9 月 29 日
I have been using importdata to read an excel sheet with time stamps. The time stamp on excel sheet is 1/1/2015 12:20:18 AM. When importdata is used, this time stamp gets converted to [42005.014097222]. I tried using datestr([42005.014097222],31) command and this is being converted to '0115-01-02 00:20:18'; but it has to be 2015-01-01 00:20:18'. How can this be fixed? Thank you!
Here is the program:
[filename, pathname] = uigetfile({'*.*'},'Choose the input file');
delimiterIn = ',';
headerlinesIn = 0;
A = importdata([pathname filename],delimiterIn,headerlinesIn); %this is a long array of time stamps, just citing one in the example above.
start_time=A(:,1); %start_time(1)=[42005.014097222]; but the 1st element in the excel sheet is 1/1/2015 12:20:18 AM
stop_time=A(:,2);
[y1,m1,d1,h1,m1,s1]=datevec(datestr(start_time,31)); %the result here gives me y1=115 and d1=2; but the right result should be y1=2015 and d1=1.
[y2,m2,d2,h2,m2,s2]=datevec(datestr(stop_time,31));

採用された回答

Ajay Kumar
Ajay Kumar 2019 年 9 月 28 日
編集済み: Ajay Kumar 2019 年 9 月 29 日
The default date for excel is 1899/12/30_00:00:00. So you have to add this to the date number to get the correct date.
Try this
t1 = datenum('1899/12/30_00:00:00','yyyy/mm/dd_HH:MM:SS');
datestr(t1+42005.014097222)
Please accept the answer if you got what you need. Thanks :)
  5 件のコメント
Danya Pradeep Kumar
Danya Pradeep Kumar 2019 年 9 月 29 日
Thank you, Stephen! That makes sense.
Ajay Kumar
Ajay Kumar 2019 年 9 月 29 日
Corrected. Thanks for the info Stephen :)

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

その他の回答 (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