Formatting dates after using textscan

1 次查看(过去 30 天)
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!

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by