Matlab:读取多种格式的txt文件并跳过字符串和void行

时间:2017-05-08 22:02:25

标签: matlab readfile

我有30个.txt文件,格式如下所示。从每个文件,我需要提取相应的值和矩阵,因此,我需要跳过字符串行和空行。我也知道每个时间步长的每个n乘n矩阵的总计数(这种情况下我们有5个5x5矩阵)。但是,我的代码不起作用。

这是txt文件的内容

 Current Time Step =            2

 Array - one

           1           2           3           4           5
           2           0           1           0           0
           3           1           0           0           0
           4           0           0           0           1
           5           0           0           1           0

 Array - two

           1           1           1           1           1
           2           0           1           0           0
           3           1           0           0           0
           4           0           0           0           1
           5           0           0           1           0

 Array - three

           1  0.000000000000000E+000  0.000000000000000E+000
           2  -2.43840000000000       0.000000000000000E+000
           3   2.43840000000000       0.000000000000000E+000
           4  0.000000000000000E+000  -2.43840000000000     
           5  0.000000000000000E+000   2.43840000000000     

 Array - four

           1  8.969565865552799E-004
           2  3.871274684514957E-004
           3  3.871274684514958E-004
           4  3.871274684514958E-004
           5  3.871274684514956E-004

 Array - five

           1   35472082.4364420     
           2   34502005.6533170     
           3   34502005.6533170     
           4   34502005.6533170     
           5   34502005.6533170     

 Time of Current Time Step =   0.537921191784371     

 Time at the End of Current Time Step =   0.559240045256864 

这是我的代码:

Nc = 5;
textFilename = ['TimeStep-2.txt'];
fid = fopen(textFilename);

for k = 1:1
    zero = sscanf(fid,'%f','Delimiter','\n');
end

for k = (1+4):(1+(Nc-1)+4)
    one = sscanf(fid,'%f %f %f %f %f',[5,inf]);
end

for k = (1+(Nc-1)+2*4):(1+2*(Nc-1)+2*4)
    two = sscanf(fid,'%f %f %f %f %f',[5,inf]);
end

for k = (1+2*(Nc-1)+3*4):(1+3*(Nc-1)+3*4)
    three = sscanf(fid,'%f %f %f',[3,inf]);
end

for k = (1+3*(Nc-1)+4*4):(1+4*(Nc-1)+4*4)
    four = sscanf(fid,'%f %f',[2,inf]);
end

for k = (1+4*(Nc-1)+5*4):(1+5*(Nc-1)+5*4)
    five = sscanf(fid,'%f %f',[2,inf]);
end

for k = (1+5*(Nc-1)+5*4+2):(1+5*(Nc-1)+5*4+2)
    six = sscanf(fid,'%f','Delimiter','\n');
end

for k = (1+5*(Nc-1)+5*4+2*2):(1+5*(Nc-1)+5*4+2*2)
    seven = sscanf(fid,'%f','Delimiter','\n');
end

fclose(fid);

1 个答案:

答案 0 :(得分:1)

我使用SenderResponse一次读取单行,然后fgetlstrtrim删除空格并将其拆分。最后我转换成双打:

strsplit

你得到:

% open text file
fid = fopen('data.txt');
% initialize struct
s = struct([]);
% initialize loop variables
incArrayIdx = false; % increment array index
arrayIdx = 1; 
rowIdx = 1;
% read single line from file (removing '\n')
tline = fgetl(fid);
while ischar(tline) % while is not EOF
    % trim white space at the beginning of line
    tline = strtrim(tline);
    % split lines into cells by spaces(default delimiter)
    C = strsplit(tline);
    % convert each cell from string to double
    A = cellfun(@str2double,C);
    if any(isnan(A)) % nans come from non-numeric data
        rowIdx = 1; % reset row index
        if incArrayIdx % if need to increase array index
            arrayIdx = arrayIdx + 1;
            incArrayIdx = false;
        end
    else % didn't find nans - only numeric data
        % next time you find nan increase array index
        incArrayIdx = true; 
        % set new row in array
        s(arrayIdx).data(rowIdx,:) = A;
        % increase row idx
        rowIdx = rowIdx + 1;
    end
    % read next line
    tline = fgetl(fid);
end
% close file
fclose(fid);

顺便说一下,你的3个最后几个矩阵似乎不是5x5,这可能会导致你出现这些问题吗?