필터 지우기
필터 지우기

Formatting dates after using textscan

조회 수: 1 (최근 30일)
Lauren
Lauren 2015년 6월 3일
편집: per isakson 2017년 6월 6일
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