如果索引是有序的像1:n
(或任何其他已知的順序),您可以使用此:
fileID = fopen('bin16.txt');
raw = fscanf(fileID,'%s');
fclose(fileID);
colon_ind = strfind(raw,'::');
data = table((1:numel(colon_ind)).',...
cell(numel(colon_ind),1),...
'VariableNames',{'Index','Pattern'});
counter = 1;
for k = colon_ind
data.Pattern(counter,:) = {raw(k+2:k+17)};
counter = counter+1;
end
,並得到這個:
data =
Index Pattern
_____ __________________
1 '0000000000000011'
2 '0000000000000101'
3 '0000000000001001'
4 '0000000000000110'
5 '0000000000010001'
6 '0000000000001010'
7 '0000000000000111'
8 '0000000000010010'
9 '0000000000001100'
10 '0000000000001011'
11 '0000000000100001'
12 '0000000000010100'
如果索引列是不是命令你可以使用這樣的事情:
fileID = fopen('bin16b.txt','r');
raw = fscanf(fileID,'%s');
C = textscan(raw,'%s','delimiter','::');
fclose(fileID);
N = (size(C{1},1)-1)/2;
data = table(zeros(N,1),cell(N,1),...
'VariableNames',{'Index','Pattern'});
data.Index(1) = str2num(C{1}{1,1});
for k = 1:N
if k>1
data.Index(k) = str2num(C{1}{k*2-1,:}(17:end));
end
data.Pattern(k,:) = {C{1}{k*2+1,:}(1:16)};
end
獲得(與我輸入的一些任意數據):
Index Pattern
_____ __________________
54 '0000000000000011'
6 '0000000000000101'
3 '0000000000001001'
4 '0000000000000110'
5 '0000000000010001'
92 '0000000000001010'
7 '0000000000000111'
35 '0000000000010010'
9 '0000000000001100'
10 '0000000000001011'
11 '0000000000100001'
12 '0000000000010100'
編輯
如果 '::' 在文件可以被刪除之前,那麼你可以使用:
text = load ('bin16b.txt', '-ascii');
data = table(text(:,1),text(:,2:end),...
'VariableNames',{'Index','Pattern'});
,並得到:
data =
Index Pattern
_____ _____________
54 [1x16 double]
6 [1x16 double]
3 [1x16 double]
4 [1x16 double]
5 [1x16 double]
92 [1x16 double]
7 [1x16 double]
35 [1x16 double]
9 [1x16 double]
10 [1x16 double]
11 [1x16 double]
12 [1x16 double]
其中模式列中的每個單元格都是這樣的:
>> data.Pattern(1,:)
ans =
Columns 1 through 14
0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 15 through 16
1 1
您如何期望將16列數據放入第二列? – Suever
您可以將其視爲文本(例如''7''0000000000000111')或使用單元格數組或表格。 – EBH