Problems with datestr()

10 次查看(过去 30 天)
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 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by