How to read csv file with asterix
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
Hello,
The NOAA csv data file has the following format (showing only the first 5 rows):
USAF  WBAN YR--MODAHRMN DIR SPD GUS CLG SKC L M H  VSB WW WW WW W TEMP DEWP    SLP   ALT    STP MAX MIN PCP01 PCP06 PCP24 PCPXX SD
037683 ***** 201001010020 010  10 ***  34 BKN * * *  7.0 ** ** ** *   36   30 ****** 29.56 ****** *** *** ***** ***** ***** ***** **
037683 ***** 201001010050 010   9 *** *** SCT * * *  7.0 ** ** ** *   36   30 ****** 29.56 ****** *** *** ***** ***** ***** ***** **
037683 ***** 201001010120 020   9 *** 722 SCT * * *  7.0 ** ** ** *   36   30 ****** 29.56 ****** *** *** ***** ***** ***** ***** **
037683 ***** 201001010150 360   9 ***  30 OVC * * *  7.0 ** ** ** *   36   30 ****** 29.56 ****** *** *** ***** ***** ***** ***** **
This one has 28 columns (but it can vary). It has all mixed - for example, the column 'CLG' has values [34, *, 722, 30, ...]. How to extract a particular column, and yet be able to skip the row containing the asterix data? One entire row can be asterix - which would mean that no data is available for that particular day. If the index of the skipped row containing asterix is known, that would be beneficial.
Thanks.
댓글 수: 0
채택된 답변
  dpb
      
      
 2014년 5월 1일
        Actually, it's not too bad...
>> fid=fopen('noaa.csv','r');
>> l=fgetl(fid);                         % the header
>> toks=tokens(l);ntok=length(toks);     % how many columns?
>> c=textscan(fid,repmat('%s',1,ntok),'collectoutput',true)  % read that many as strings
c = 
  {4x28 cell}
>> fid=fclose(fid);
Now begin to do something w/ the data...
>> [~,iclg]=intersect(toks,'CLG','rows')   % find a particular variable location
iclg =
   7
>> clg=str2double(c{1}(:,iclg))              % convert to numeric
clg =
  34
 NaN
 722
  30
>> 
find(isnan())
will return locations of missing for however you wish to deal with them.
This is one place where the cell array helps, for sure...
댓글 수: 5
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!