Converting a date in character format to an actual date recognized by MATLAB
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Ryan McBurney
il 17 Set 2019
Commentato: Ryan McBurney
il 17 Set 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 Commenti
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.
Risposta accettata
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!