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
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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!