Borrar filtros
Borrar filtros

Formatting dates after using textscan

1 visualización (últimos 30 días)
Lauren
Lauren el 3 de Jun. de 2015
Editada: per isakson el 6 de Jun. de 2017
Hello! I am trying to use textscan to to bring in a csv with a column of dates in the format mm/dd/yyyy HH:MM. I would like the end result to just be a column of Matlab dates (the HH:MM does not matter, I just need the actual day). My textscan statement works fine, but I have tried using the datenum function after it to format the dates the way I want them. However, I run into the error, "The input to DATENUM was not an array of strings." I'm not sure how exactly to fix this. I have provided my relevant code below.
sp_textscan = textscan(sp,'%s %f','HeaderLines',1,'Delimiter',',');
sp_formatted = datenum(sp_textscan(:,1),'mm/dd/yyyy');
Any help would be greatly appreciated! Thanks!

Respuesta aceptada

Guillaume
Guillaume el 3 de Jun. de 2015
sp_textscan should be a 1x2 cell array, the first element, a cell array of strings (your dates) and the second a vector of numbers. So the correct syntax for datenum would be:
sp_formatted = datenum(sp_textscan{1}, 'mm/dd/yyyy');
However, assuming 2014b or latter, even better would be to tell textscan to parse the first part as a date straightaway:
sp_textscan = textscan(sp, '%{MM/dd/yyyy}D %f', 'HeaderLines', 1, 'Delimiter', ','); %note the different format string for datetime
  1 comentario
Lauren
Lauren el 3 de Jun. de 2015
Thank you very much! I actually have 2013b, so the first one worked perfectly.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by