2017-05-08 34 views
0

以下に示すような形式の.txtファイルが30個あります。各ファイルから、対応する値と行列を抽出する必要があります。したがって、文字列線をスキップして線を空白にする必要があります。私はまた、各時間ステップ(この場合、我々は5×5行列を有する)ごとに各n行n列の行列の合計カウント数を知っている。しかし、私のコードは動作しません。Matlab:複数形式のtxtファイルを読み込み、文字列と空白行をスキップ

ここでここで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); 
+0

コードはどこで失敗しますか? – codeaviator

+0

sscanfを使用しているエラー「入力引数が多すぎます」 – FortranFun

+0

なぜこのようなデータ形式を使用していますか? – Bernhard

答えて

1

私は空白を削除するには一度に一つの行を読み取るためにfgetlを使用して、strtrimstrsplitとそれを分割する。

% 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); 

とあなたが得る:最後に、私はダブルスに変換

{s.data}' = 

    5×1 cell array 

    [5×5 double] 
    [5×5 double] 
    [5×3 double] 
    [5×2 double] 
    [5×2 double] 

をところで、多分これはあなたの問題を引き起こし、あなたの3つの最後の行列は5x5のではないようですか?

+0

あなたのコードは役に立ちますが、私はまた、最初の行と最後の2行から値を抽出したいと思います。 3つの単一値があります。 – FortranFun

関連する問題