MATLAB: Formatting dates after using textscan

date formattingdatenumtextscan

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!

Best Answer

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