Problems with datestr()

조회 수: 12 (최근 30일)
Arthur Romeu
Arthur Romeu 2020년 1월 13일
답변: Star Strider 2020년 1월 13일
Hello everyone, I am currently trying to use datestr on datetimes
string_data = datestr(data_datetime);
however it returns an error as follows:
Subscript indices must either be real positive integers or logicals.
Error in formatdate (line 157)
month = char(strrep(month(dtvector(:,2)), '.', '')); %remove period
Error in dateformverify (line 32)
S = char(formatdate([y,mo,d,h,minute,s],dateformstr,islocal));
Error in datestr (line 199)
S = dateformverify(dtnumber, dateformstr, islocal);
Error in datetime/datestr (line 768)
s = datestr(datenum(this),varargin{:});
I don't undertand why it doesn't work :(
I've attached the .mat file along with this question.
Thanks in advance.
Arthur.

채택된 답변

Star Strider
Star Strider 2020년 1월 13일
The problem is that 25 of ‘data_datetime’ is NaT (not a time, analogous to NaN, not a number). The datenum function cannot convert NaT to anything.
D = load('datetimes.mat');
dt = D.data_datetime;
natv = nnz(isnat(dt))
produces:
natv =
25
One option is to simply interpolate the times:
dtfm = fillmissing(dt,'linear');
s = datestr(datenum(dtfm));
That runs without error.
See the documentation on fillmissing to explore other interpolation options.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by