matlab scantxt,使用textscan容納.txt文件中的空白條目 - MATLAB

%// 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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值