2016-08-08 9 views
1

オクターブまたはmatlabのデータ処理に関する質問があります。以下のように流暢からエクスポート 私が持っているファイルは:オクターブ/ matlabテキストファイルを1行ずつ読み込み、数値のみを行列に保存します

     "Surface Integral Report" 

     Mass-Weighted Average 
      Static Temperature     (k) 

  crossplane-x-0.001   1242.9402 
      crossplane-x-0.025   1243.0017 
      crossplane-x-0.050   1243.2036 
      crossplane-x-0.075   1243.5321 
      crossplane-x-0.100   1243.9176 

そして私は、後処理のためにオクターブ/ MathWorks社のMATLABを使用します。 最初に行を読み、 "crossplane-x-"の行だけを新しいファイルに保存するか、またはその行のデータを直接行列に保存します。似たようなファイルがたくさんあるので、タイトルを呼び出すだけでプロットすることができます。 しかし、 "crossplane-x-"という文字が含まれている行を特定することには問題があります。私は次のようなことをしようとしています。

clear, clean, clc; 
% open a file and read line by line 
fid = fopen ("h20H22_alongHGpath_temp.dat"); 
% save full lines into a new file if only chars inside 
txtread = fgetl (fid) 
num_of_lines = fskipl(fid, Inf); 
char = 'crossplane-x-' 
for i=1:num_of_lines, 
    if char in fgetl(fid) 
    [x, nx] = fscanf(fid); 
    print x 
    endif 
endfor 
fclose (fid); 

誰もがこの問題を晴らしていますか?私は正しい機能を使用していますか?ありがとうございました。

+0

は、私はあなたにアンディに感謝のgrepやawkは、このタスク – Andy

+0

ため、より適していると思う、私はsedを使用します... – BeYo

答えて

0

はここにあなたの特定のファイルのための簡単な方法です:

>> S = fileread("myfile.dat");  % collect file contents into string 
>> C = strsplit(S, "crossplane-x-"); % first cell is the header, rest is data 
>> M = str2num (strcat (C{2:end})) % concatenate datastrings, convert to numbers 
M = 

    1.0000e-03 1.2429e+03 
    2.5000e-02 1.2430e+03 
    5.0000e-02 1.2432e+03 
    7.5000e-02 1.2435e+03 
    1.0000e-01 1.2439e+03 
関連する問題