Problems with datestr()

3 Ansichten (letzte 30 Tage)
Arthur Romeu
Arthur Romeu am 13 Jan. 2020
Beantwortet: Star Strider am 13 Jan. 2020
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.

Akzeptierte Antwort

Star Strider
Star Strider am 13 Jan. 2020
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.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by