필터 지우기
필터 지우기

Date Parsing Error for Time 00:00:00 from MS Excel

조회 수: 3 (최근 30일)
Motasem Mustafa
Motasem Mustafa 2020년 10월 22일
댓글: Peter Perkins 2020년 11월 18일
Dears,
I am using the code below to do parsing for date-time cells in an MS Excel sheet with date-time form of ( 01/05/2019 00:00) as in the screenshot below.
clc,clear,close all;
[num1,data] = xlsread('Book_new.xlsx','sheet1','A1:A30');
a=datevec(data,'dd/mm/yyyy HH:MM:SS');
date=datestr(datenum(a),'dd/mm/yyyy');
time=datestr(datenum(a),'HH:MM:SS');
Year=datestr(datenum(a),'yyyy');
mm=datestr(datenum(a),'mm');
dd=datestr(datenum(a),'dd');
yy=datestr(datenum(a),'yyyy');
[status,message] =xlswrite('motasem.xlsx',str2num(yy),'sheet1','A1:A30');
[status,message] =xlswrite('motasem.xlsx',str2num(mm),'sheet1','B1:B30');
[status,message] =xlswrite('motasem.xlsx',str2num(dd),'sheet1','C1:C30');
[status,message] =xlswrite('motasem.xlsx',string(time),'sheet1','D1:D30');
When I run the code for example for the 1st 30 readings (half hourly readings) it gives me the following error :
"Error using dtstr2dtvecmx
Failed to convert from text to date number.
Error in datevec (line 123)
y = dtstr2dtvecmx(t,icu_dtformat);
Error in motasem (line 4)
a=datevec(data,'dd/mm/yyyy HH:MM:SS');"
But when I change the range of data to avoid the first reading which contains the time 00:00:00 it works and gives the below output :
Any suggestions please ?
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 10월 22일
As usual our suggestion is to use readtable instead of xlsread.
Motasem Mustafa
Motasem Mustafa 2020년 10월 23일
Thanks I have solved this using your suggestion of using readtable :)

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

답변 (1개)

Motasem Mustafa
Motasem Mustafa 2020년 10월 23일
The new code that worked .
Thanks @ Walter Roberson
clc,clear,close all;
data = readtable('Book_new.xlsx','Range','A1:A60','ReadVariableNames',false);
A = table2array(data);
yy=datestr(datenum(A),'yyyy');
mm=datestr(datenum(A),'mm');
dd=datestr(datenum(A),'dd');
time=datestr(datenum(A),'HH:MM:SS');
[status,message] =xlswrite('motasem.xlsx',str2num(yy),'sheet1','A1:A30');
[status,message] =xlswrite('motasem.xlsx',str2num(mm),'sheet1','B1:B30');
[status,message] =xlswrite('motasem.xlsx',str2num(dd),'sheet1','C1:C30');
[status,message] =xlswrite('motasem.xlsx',string(time),'sheet1','D1:D30');
  댓글 수: 3
Motasem Mustafa
Motasem Mustafa 2020년 10월 26일
Thanks alot for your support :)
Peter Perkins
Peter Perkins 2020년 11월 18일
Yes, as Walter says, don't use datenum. There are datetime methods like year and ymd for all of those things you are exporting. You do not need to convert datetimes to datenums, to datestrs, to numeric.
Also, I strongly recommend that you look at writetable instead of xlswrite. Create separate y/m/d and time of day variables in the table, and you can write it all out in one line.

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

카테고리

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