Converting a date in character format to an actual date recognized by MATLAB

조회 수: 1 (최근 30일)
I pulled the date from a cell in an excel file that looked like this "Date: 06-Jun-2019 start" without the quotes and shortened it to 06-Jun-2019.
I was originally just putting this date as an annotation on a plot, but now I am running several excel files through the code and I want to collect these dates in a variable called AllDates. When I set this up the code fails because 06-Jun-2019 is sized 1x11 and I'm trying to fill a single index of AllDates.
How can I change the format of 06-Jun-2019 so MATLAB understands its a date. Eventually I need to plot these dates as well.
Here is some code I am using:
for k = 1:numel(Filename)
File = fullfile(Path, Filename{k});
[num, txt, raw] = xlsread(File);
DateInfo = cell2mat(raw(2,1));
EventDate = DateInfo(1,7:end-6);
EventDate = datetime(EventDate,'InputFormat', 'dd-mm-yyyy');
allDate(k) = EventDate
end
  댓글 수: 3
Ryan McBurney
Ryan McBurney 2019년 9월 17일
Nice, that helped a lot. Thanks. I changed how I initialized allDate. I accidentally left that out of my code for the sake of less clutter.
allDate = datetime(zeros(1,length(Filename)));
allDate(k) = EventDate;
I also changed the first line of this part of code to how you noted in your answer.
DateInfo = raw{2,1};
EventDate = DateInfo(1,7:end-13);
EventDate = datetime(EventDate,'InputFormat', 'dd-MM-yyyy');
The code doesn't fail, but my allDate output looks like this
allDate =
[30-Nov--0001 00:00:00, NaT, NaT, NaT, NaT, 06-Jun-2019 00:00:00 ]
A few more head scratches and I'll get there, unless you have more hints.
Thanks again, Guillaume.
Walter Roberson
Walter Roberson 2019년 9월 17일
allDate = datetime(zeros(1,length(Filename)));
would be an error if length(Filename) was not 3 or 6, and for either of those it would create a scalar datetime matrix. If you wanted to create length(Filename) datetimes then you would need that as the number of rows, such as datetime(zeros(length(Filename),3))
But
allDate = NaT(1, length(Filename));
would be better for that situation.

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

채택된 답변

Guillaume
Guillaume 2019년 9월 17일
Your
allDate = datetime(zeros(1,length(Filename)));
will only work if length(Filename) is 3 or 6. Otherwise you'll get an error. Even if you don't get an error, your array is initialised with just one datetime: 30 Nov 1 00:00:00
The simplest way to initialise it would be:
allDate = NaT(size(Filename));
which will create a datetime array full of Not A Time.
The code doesn't fail, but my allDate output looks like this
I would expect that if k is 6 and never goes through 1 to 5. allDate consists of that initial datetime(0, 0, 0) you put in there at index 1, NaT (not a time) for flling the indices between 1 and 6, and the value you put in at index 6.
  댓글 수: 1
Ryan McBurney
Ryan McBurney 2019년 9월 17일
This did the trick:
allDate = NaT(size(Filename));
I can't belive I didn't realize I was initializing allDate inside the loop.
It's been a long day.
Thanks so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by