3

このトピックについては、すでに多くの質問があることを知っています。ただし、すべてのソリューションは特定のケース向けです。を含むセル配列から数値データを抽出する高度に一般化された方法を見つけようとしていますchar

サンプルデータ: lines= <779x88 char>(後処理textscanデータ)

xtotal=   0.3414E-03   ytotal=   0.0000E+00  ztotal=   0.0000E+00   etotal=   0.0000E+00
xtotal=   0.7239E-03   ytotal=   0.0000E+00  ztotal=   0.0000E+00   etotal=   0.1875E-08
...
xtotal=   0.1788E-01   ytotal=   0.0000E+00  ztotal=   0.0000E+00   etotal=   0.9965E-06
xtotal=   0.2586E-01   ytotal=   0.0000E+00  ztotal=   0.0000E+00   etotal=   0.1992E-05

次のループは、私がやりたいことをしています:

   n =  4;  %number of output values
off1 =  3;  %offset 1
off2 = 13;  %offset 2
L = size(lines,1);   %Length of cell array

index = strfind(lines(1,:),'=');   %find all indices in char for "="
value = zeros(L,n);                %pre-allocation

% read numerical values and write it into array
% for sure vectorization is possible, but that shouldn't be the topic now
for ii=1:L
    for jj=1:n  
    value(ii,jj) = str2double( lines(ii, index(jj)+off1:index(jj)+off2 ) );
    end
end

結果:

value = 

  0.0003       0         0         0
  0.0007       0         0    0.0000
...
183.1000       0         0   95.4900
183.1000       0         0   95.4900

この場合は問題なく機能しますが、次のように定義する必要があります。

   n =  4;  %number of output values
off1 =  3;  %offset 1
off2 = 13;  %offset 2

私が処理しているすべての異なる入力ファイルについて決定したくありません。=すべての入力ファイルで同じである必要があるため、事前に決定されていると想定しているのは区切り文字 " " だけです。では、char 配列から数値データを検出する信頼できる方法はありませんか?

4

1 に答える 1