Problems with datestr()

3 visualizzazioni (ultimi 30 giorni)
Arthur Romeu
Arthur Romeu il 13 Gen 2020
Risposto: Star Strider il 13 Gen 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.

Risposta accettata

Star Strider
Star Strider il 13 Gen 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.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by