Converting a date in character format to an actual date recognized by MATLAB
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ryan McBurney
am 17 Sep. 2019
Kommentiert: Ryan McBurney
am 17 Sep. 2019
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 Kommentare
Walter Roberson
am 17 Sep. 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.
Akzeptierte Antwort
Guillaume
am 17 Sep. 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 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!