Reading text file of different columns as empty in matlab
조회 수: 3 (최근 30일)
이전 댓글 표시
My text file consists on 31*13 matrix, Problem is 3nd column which has length of 28, columns no 3,5,8,10,12 are of length 30, Actually this is daily temperature data for one year, I tried, but getting different biases?
Possible read it in matlab, and place NaN value where empty value appear?
Text file has been attached. Looking to hear from you people. Thanks
댓글 수: 0
채택된 답변
per isakson
2016년 9월 14일
편집: per isakson
2016년 9월 15일
It used to be tricky to read files like this one with Matlab. I don't know whether The Mathworks provided a solution in a recent release, but I don't think so. I attach a file, which is on-going-work, but it seems to do the job. The file may be called in two ways.
cac = read_fixed_format( 'CHLMIN00.TXT' ...
, '%10f%9f%9f%9f%9f%9f%9f%9f%9f%9f%9f%9f%9f' ...
, 'Headerlines',1 );
cac = read_fixed_format( 'CHLMIN00.TXT', '%10f12(%9f)', 'Headerlines',1 );
test
>> cac{3}'
ans =
Columns 1 through 9
3.4000 3.9000 4.4000 3.1000 2.8000 2.2000 3.1000 3.6000 3.5000
Columns 10 through 18
3.9000 4.2000 3.9000 3.3000 3.3000 5.0000 6.1000 5.0000 5.6000
Columns 19 through 27
4.4000 3.9000 2.8000 3.9000 4.7000 3.9000 3.9000 4.4000 5.0000
Columns 28 through 31
5.0000 5.3000 NaN NaN
 
Does MATLAB offer a user friendly way to read  CHLMIN00.TXT ?   I made the test below with R2016a.
YES, interactively Import Data gets the "empty entries" right, but it seems as is the first empty line of the text file ends up as a row of NaNs at the bottom of the data.
 
HOWEVER, the function, importdata, makes a mess of the "empty entries". On the other hand, it's not confused by the first empty line.
>> data = importdata( 'CHLMIN00.TXT' );
>> whos data
Name Size Bytes Class Attributes
data 31x13 3224 double
>> data(26:end,3)'
ans =
4.4000 5.0000 5.0000 5.3000 12.2000 10.1000
>>
I'm not amused!
댓글 수: 13
per isakson
2016년 9월 16일
편집: per isakson
2016년 9월 16일
"to know this file is tab-delimated " and "multiple text files at once"   Try this
>> [ has, cnt ] = has_tabs( 'ASTMAX93.TXT' )
has =
0
cnt =
0
>> [ has, cnt ] = has_tabs( 'SKDMIN02.TXT' )
has =
1
cnt =
372
where
function [ has, cnt ] = has_tabs( filespec )
str = fileread( filespec );
ist = sprintf('\t') == str;
has = any( ist );
cnt = sum( double( ist ) );
end
per isakson
2016년 9월 17일
편집: per isakson
2016년 9월 17일
"correct format string"   Try
>> format_spec = search_format_specifier( 'SKDMIN02.TXT' )
Error using search_format_specifier (line 20)
The column widths of the rows 2 through 28 in "SKDMIN02.TXT" differ
>>
>> format_spec = search_format_specifier( 'ASTMAX93.TXT' )
format_spec =
%7f%6f%6f%6f%6f%6f%6f%6f%6f%6f%6f%6f
>>
>> format_spec = search_format_specifier( 'CHLMIN00.TXT' )
format_spec =
%10f%9f%9f%9f%9f%9f%9f%9f%9f%9f%9f%9f
where
function format_spec = search_format_specifier( filespec, rows )
%
narginchk( 1, 2 )
if nargin == 1
rows = [ 2, 28 ];
end
%
fid = fopen( filespec, 'r' );
cac = textscan( fid, '%s', diff(rows)+1, 'Whitespace','' ...
, 'Delimiter','\n', 'Headerlines',rows(1)-1 );
[~] = fclose( fid );
%
cix = regexp( cac{1}, '(?<=\d)([ ]{1}|$)', 'start' );
cix = unique( cell2mat( cix ), 'rows' );
%
assert( size(cix,1) == 1, 'search_format_specifier:NotFixedWidth' ...
, 'The column widths of the rows %d through %d in "%s" differ' ...
, rows(1), rows(2), filespec )
%
col_width = diff([ 1, cix ]);
format_spec = sprintf( '%%%df', col_width );
end
추가 답변 (1개)
KSSV
2016년 9월 14일
On using
data = importdata(txtfile) ;
in data default wherever data is missing NaN is introduced. I tried this in MATLAB2015a.
참고 항목
카테고리
Help Center 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!