Problems with datestr()

3 visualizaciones (últimos 30 días)
Arthur Romeu
Arthur Romeu el 13 de En. de 2020
Respondida: Star Strider el 13 de En. de 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.

Respuesta aceptada

Star Strider
Star Strider el 13 de En. de 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.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by