Reading text file of different columns as empty in matlab

20 views (last 30 days)
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

Accepted Answer

per isakson
per isakson on 14 Sep 2016
Edited: per isakson on 15 Sep 2016
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
&nbsp
Does MATLAB offer a user friendly way to read&nbsp CHLMIN00.TXT ? &nbsp 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.
&nbsp
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 Comments
per isakson
per isakson on 16 Sep 2016
Edited: per isakson on 16 Sep 2016
"to know this file is tab-delimated " and "multiple text files at once" &nbsp 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
per isakson on 17 Sep 2016
Edited: per isakson on 17 Sep 2016
"correct format string" &nbsp 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

Sign in to comment.

More Answers (1)

KSSV
KSSV on 14 Sep 2016
On using
data = importdata(txtfile) ;
in data default wherever data is missing NaN is introduced. I tried this in MATLAB2015a.
  1 Comment
Muhammad Usman Saleem
Muhammad Usman Saleem on 14 Sep 2016
I have tried , but in 3rd column importdata is just interpolating dataset, not producing nan for missing values. Plz try this by ur hands plz

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!