필터 지우기
필터 지우기

How to textscan dates and data delimited with spaces?

조회 수: 11 (최근 30일)
David
David 2021년 10월 25일
댓글: Jeremy Hughes 2021년 10월 25일
I have data like this:
2010 12 31 23 50 198 8.2 999 99.0 9999
2011 01 01 00 00 199 8.1 199 8.5 2347
2011 01 01 00 10 198 8.4 999 99.0 9999
How would I parse the lines into dates and data?
Ideally I would expect something like the below to parse it into dates and data:
textscan('2010 12 31 23 50 198 8.2 999 99.0 9999','%16{yyyy MM dd HH mm}D %f %f %f %f')
But the spaces in the dates seem to be treated as delimiters and only the first field is passed to the date parser.
Is it possible to do this without preprocessing?
I found I could parse the space-formatted date by disabling the space delimiters, but then I couldn't parse later fields on the line unless I added delimiters:
>> textscan('2010 12 31 23 50,198,8.2,999,99.0,9999','%{yyyy MM dd HH mm}D %f %f %f %f','Delimiter',',')
ans =
1×5 cell array
{2×1 datetime} {[198]} {[8.2000]} {[999]} {[99]}
>>
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2021년 10월 25일
Is your original data a matrix, cell array, character array, or string array?
David
David 2021년 10월 25일
The original is a gzipped text file with a header from NOAA at https://www.ndbc.noaa.gov/station_page.php?station=chlv2
% head ~/Downloads/chlv2c2011.txt
#YY MM DD hh mm WDIR WSPD GDR GST GTIME
#yr mo dy hr mn degT m/s degT m/s hhmm
2010 12 31 23 10 202 7.5 999 99.0 9999
2010 12 31 23 20 204 7.5 999 99.0 9999
2010 12 31 23 30 204 7.5 999 99.0 9999

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 10월 25일
Assuming your data is numeric, I would do this.
data = [2010 12 31 23 50 198 8.2 999 99.0 9999;
2011 01 01 00 00 199 8.1 199 8.5 2347;
2011 01 01 00 10 198 8.4 999 99.0 9999];
Dates = datetime(data(:,1),data(:,2),data(:,3),data(:,4),data(:,5),0);
final=table(Dates,data(:,6:end));
final = splitvars(final,2,'NewVariableNames',{'A','B','C','D','E'})
final = 3×6 table
Dates A B C D E ____________________ ___ ___ ___ ___ ____ 31-Dec-2010 23:50:00 198 8.2 999 99 9999 01-Jan-2011 00:00:00 199 8.1 199 8.5 2347 01-Jan-2011 00:10:00 198 8.4 999 99 9999
  댓글 수: 1
Jeremy Hughes
Jeremy Hughes 2021년 10월 25일
This is also probably faster for converting the dates that using the %D format.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chris
Chris 2021년 10월 25일
Another option, for an input string:
temp = split('2010 12 31 23 50,198,8.2,999,99.0,9999',',');
Date = datetime(temp{1},'InputFormat','yyyy MM dd HH mm');
data = temp(2:end);
(But readmatrix() to import the data in numeric format is probably a better idea)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by