why textscan can't parse the time like this

조회 수: 3 (최근 30일)
lang
lang 2025년 4월 8일
편집: Stephen23 2025년 4월 8일
tunit='days since 2024-10-31 18:00:00';
tunit = 'days since 2024-10-31 18:00:00'
rn=textscan(tunit,'%s since %{yyyy-MM-dd HH:mm:ss}D',1);
Error using textscan
Unable to read the DATETIME data with the format "yyyy-MM-dd HH:mm:ss". If the data is not a time, use %q to get text data.

채택된 답변

Stephen23
Stephen23 2025년 4월 8일
편집: Stephen23 2025년 4월 8일
"why textscan can't parse the time like this"
Because you have a delimiter right in the middle of your datestamp. Clearly that will not work: TEXTSCAN always splits at delimiters (because that is the meaning of a delimiter). Also note that you explicitly wrote delimiters in the format string, but with TEXTSCAN you specify the delimiter (or use the default) and then do NOT write it in the format string.
You can parse the date & time as DATETIME & DURATION objects and then add them together:
tunit = 'days since 2024-10-31 18:00:00';
rn = textscan(tunit,'%ssince%{y-M-d}D%{hh:mm:ss}T')
rn = 1x3 cell array
{1x1 cell} {[2024-10-31]} {[18:00:00]}
dt = rn{2}+rn{3};
dt.Format = 'yyyy-MM-dd HH:mm:ss'
dt = datetime
2024-10-31 18:00:00
Or using the automagic DATETIME & DURATION format detection:
rn = textscan(tunit,'%ssince%D%T');
dt = rn{2}+rn{3};
dt.Format = 'yyyy-MM-dd HH:mm:ss'
dt = datetime
2024-10-31 18:00:00

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by