Date Parsing Error for Time 00:00:00 from MS Excel
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/389248/image.jpeg)
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 :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/389253/image.jpeg)
Any suggestions please ?
댓글 수: 2
답변 (1개)
Motasem Mustafa
2020년 10월 23일
댓글 수: 3
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 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!