Filter löschen
Filter löschen

Formatting dates after using textscan

1 Ansicht (letzte 30 Tage)
Lauren
Lauren am 3 Jun. 2015
Bearbeitet: per isakson am 6 Jun. 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!

Akzeptierte Antwort

Guillaume
Guillaume am 3 Jun. 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 Kommentar
Lauren
Lauren am 3 Jun. 2015
Thank you very much! I actually have 2013b, so the first one worked perfectly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by