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

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Ryan McBurney
Ryan McBurney il 17 Set 2019
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 il 17 Set 2019
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.

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 17 Set 2019
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 Commento
Ryan McBurney
Ryan McBurney il 17 Set 2019
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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Prodotti


Release

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by