在Matlab中读取数组中的文件 [英] Read the file in an array in matlab

查看:298
本文介绍了在Matlab中读取数组中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1 ::  0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
2 ::  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
3 ::  0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
4 ::  0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
5 ::  0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
6 ::  0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
7 ::  0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
8 ::  0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
9 ::  0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
10 ::  0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1
11 ::  0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1
12 ::  0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0

我有上面的文本文件.我想在Matlab中读取此文件并要存储 两列中数组中文件的内容.在第一列中,
序列号,另一列中的16位模式如下

I have the above text file. I want to read this file in Matlab and want to store the content of the file in an array in two columns. In first column the
sequence number and in the other column the 16 bit pattern as follows

1     0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
2     0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
3     0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
4     0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
5     0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
6     0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
7    0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1

有人可以帮助我吗?

推荐答案

如果索引按照1:n的顺序排列(或其他任何已知顺序),则可以使用以下方法:

If the indices are ordered like 1:n (or any other know order), you can use this:

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'

如果未对索引列进行排序,则可以使用以下内容:

If the index column is not ordered you can use something like this:

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

要获取(我输入了一些任意数据):

To obtain (with some arbitrary data I entered):

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'


编辑

如果可以先删除文件中的'::',则可以使用:

If the '::' in the file can be deleted before, then you can use:

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]

模式列中的每个单元格都像这样:

where each cell in the pattern column is like:

>> 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

这篇关于在Matlab中读取数组中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆