使用 textscan 容纳 .txt 文件中的空白条目 - MATLAB
Accommodating blank entries in .txt files using textscan - MATLAB
我有一个 9 列制表符分隔的 .txt 文件,其中包含多种数据格式 - 但是,“type
”中有些条目是空的。
id id_2 s1 s2 st1 st2 type desig num
1 1 51371 51434 52858 52939 5:3_4:4_6:2_4:4_2:6 CO 1
2 1 108814 108928 109735 110856 5:3_4:4_6:2_4:4_2:7 CO 2
3 1 130975 131303 131303 132066 5:3_4:4_6:2_4:4_2:8 NCO 3
4 1 191704 191755 194625 194803 NCO 4
5 2 69355 69616 69901 70006 CO 5
6 2 202580 202724 204536 205151 5:3_4:4_6:2_4:4 CO 6
由于混合格式类型,我一直使用 textscan 导入此数据:
data = textscan(fid1, '%*f %f %f %f %f %f %*s %s %*[^\r\n]','HeaderLines',1);
要获取第 2-6 列,请跳过“type
”并获取第 8 列。
此方法在具有空条目的行上失败 - 它会跳过它,就好像它不是列一样,而不是采用 'NCO' 或 'CO',而是采用“4”或“5”。
有什么办法可以避免这种情况吗?我知道我可以更改原始 .txt 文件以包含诸如 'NA' 之类的内容来表示空条目,但这不如一种更强大的读取此类文件的方法可取。
编辑:
除了下面的答案之外,简单地指定使用的分隔符似乎可以解决问题:
data = textscan(fid1, '%*f %f %f %f %f %f %*s %s %*[^\r\n]','HeaderLines',1,'delimiter','\t');
这是 importdata
and strsplit
-
的一种方法
%// Read in data with importdata
data = importdata('data1.txt') %// 'data1.txt' is the input text file
%// Split data
split_data = cellfun(@(x) strsplit(x,' '),data,'Uni',0)
N = numel(split_data) %// number of rows in input textfile
%// Setup output cell and mask arrays
out_cell = cell(9,N)
mask = true(9,N)
%// Set the "type" entry as zero in mask array for the rows in textfile
%// that has corresponding entry missing
mask(7,cellfun(@length,split_data)~=9)=0
%// Use mask to set cells in out_cell from split data entries
out_cell(mask) = [split_data{:}]
out = out_cell'
样本运行-
>> type data1.txt
id id_2 s1 s2 st1 st2 type desig num
1 1 51371 51434 52858 52939 5:3_4:4_6:2_4:4_2:6 CO 1
2 1 108814 108928 109735 110856 5:3_4:4_6:2_4:4_2:7 CO 2
3 1 130975 131303 131303 132066 5:3_4:4_6:2_4:4_2:8 NCO 3
4 1 191704 191755 194625 194803 NCO 4
5 2 69355 69616 69901 70006 CO 5
6 2 202580 202724 204536 205151 5:3_4:4_6:2_4:4 CO 6
>> out
out =
'id' 'id_2' 's1' 's2' 'st1' 'st2' 'type' 'desig' 'num'
'1' '1' '51371' '51434' '52858' '52939' '5:3_4:4_6:2_4:4_2:6' 'CO' '1'
'2' '1' '108814' '108928' '109735' '110856' '5:3_4:4_6:2_4:4_2:7' 'CO' '2'
'3' '1' '130975' '131303' '131303' '132066' '5:3_4:4_6:2_4:4_2:8' 'NCO' '3'
'4' '1' '191704' '191755' '194625' '194803' [] 'NCO' '4'
'5' '2' '69355' '69616' '69901' '70006' [] 'CO' '5'
'6' '2' '202580' '202724' '204536' '205151' '5:3_4:4_6:2_4:4' 'CO' '6'
我有一个 9 列制表符分隔的 .txt 文件,其中包含多种数据格式 - 但是,“type
”中有些条目是空的。
id id_2 s1 s2 st1 st2 type desig num
1 1 51371 51434 52858 52939 5:3_4:4_6:2_4:4_2:6 CO 1
2 1 108814 108928 109735 110856 5:3_4:4_6:2_4:4_2:7 CO 2
3 1 130975 131303 131303 132066 5:3_4:4_6:2_4:4_2:8 NCO 3
4 1 191704 191755 194625 194803 NCO 4
5 2 69355 69616 69901 70006 CO 5
6 2 202580 202724 204536 205151 5:3_4:4_6:2_4:4 CO 6
由于混合格式类型,我一直使用 textscan 导入此数据:
data = textscan(fid1, '%*f %f %f %f %f %f %*s %s %*[^\r\n]','HeaderLines',1);
要获取第 2-6 列,请跳过“type
”并获取第 8 列。
此方法在具有空条目的行上失败 - 它会跳过它,就好像它不是列一样,而不是采用 'NCO' 或 'CO',而是采用“4”或“5”。
有什么办法可以避免这种情况吗?我知道我可以更改原始 .txt 文件以包含诸如 'NA' 之类的内容来表示空条目,但这不如一种更强大的读取此类文件的方法可取。
编辑:
除了下面的答案之外,简单地指定使用的分隔符似乎可以解决问题:
data = textscan(fid1, '%*f %f %f %f %f %f %*s %s %*[^\r\n]','HeaderLines',1,'delimiter','\t');
这是 importdata
and strsplit
-
%// Read in data with importdata
data = importdata('data1.txt') %// 'data1.txt' is the input text file
%// Split data
split_data = cellfun(@(x) strsplit(x,' '),data,'Uni',0)
N = numel(split_data) %// number of rows in input textfile
%// Setup output cell and mask arrays
out_cell = cell(9,N)
mask = true(9,N)
%// Set the "type" entry as zero in mask array for the rows in textfile
%// that has corresponding entry missing
mask(7,cellfun(@length,split_data)~=9)=0
%// Use mask to set cells in out_cell from split data entries
out_cell(mask) = [split_data{:}]
out = out_cell'
样本运行-
>> type data1.txt
id id_2 s1 s2 st1 st2 type desig num
1 1 51371 51434 52858 52939 5:3_4:4_6:2_4:4_2:6 CO 1
2 1 108814 108928 109735 110856 5:3_4:4_6:2_4:4_2:7 CO 2
3 1 130975 131303 131303 132066 5:3_4:4_6:2_4:4_2:8 NCO 3
4 1 191704 191755 194625 194803 NCO 4
5 2 69355 69616 69901 70006 CO 5
6 2 202580 202724 204536 205151 5:3_4:4_6:2_4:4 CO 6
>> out
out =
'id' 'id_2' 's1' 's2' 'st1' 'st2' 'type' 'desig' 'num'
'1' '1' '51371' '51434' '52858' '52939' '5:3_4:4_6:2_4:4_2:6' 'CO' '1'
'2' '1' '108814' '108928' '109735' '110856' '5:3_4:4_6:2_4:4_2:7' 'CO' '2'
'3' '1' '130975' '131303' '131303' '132066' '5:3_4:4_6:2_4:4_2:8' 'NCO' '3'
'4' '1' '191704' '191755' '194625' '194803' [] 'NCO' '4'
'5' '2' '69355' '69616' '69901' '70006' [] 'CO' '5'
'6' '2' '202580' '202724' '204536' '205151' '5:3_4:4_6:2_4:4' 'CO' '6'