2012-03-17 3 views
1

基本的には、次のようなテキストファイルの束に複数のデータが繰り返し表示されます(2つのセクションを表示)。MATLAB:ヘッダーの4行を削除して14行ごとに削除し、2行目/ 3列目のデータをtxtファイルに保存

Iは、第2および第3の列のデータのみ10行(日付およびY値のない時間)を維持し、私はそれを書くことができるように、変数にすべてを格納するためにMATLABにおける方法が必要
t0 1/20/2012 05:41:05.068750 
delta t 0.100000 

time Y 
1/20/2012 05:41:05.068750 0.925883 
1/20/2012 05:41:05.168750 0.926540 
1/20/2012 05:41:05.268750 0.926540 
1/20/2012 05:41:05.368750 0.926540 
1/20/2012 05:41:05.468750 0.926869 
1/20/2012 05:41:05.568750 0.925225 
1/20/2012 05:41:05.668750 0.926540 
1/20/2012 05:41:05.768750 0.928185 
1/20/2012 05:41:05.868750 0.925554 
1/20/2012 05:41:05.968750 0.925225 
t0 1/20/2012 05:41:06.068750 
delta t 0.100000 

time Y 
1/20/2012 05:41:06.068750 0.926212 
1/20/2012 05:41:06.168750 0.924567 
1/20/2012 05:41:06.268750 0.926540 
1/20/2012 05:41:06.368750 0.925883 
1/20/2012 05:41:06.468750 0.914371 
1/20/2012 05:41:06.568750 0.907135 
1/20/2012 05:41:06.668750 0.906806 
1/20/2012 05:41:06.768750 0.903188 
1/20/2012 05:41:06.868750 0.902201 
1/20/2012 05:41:06.968750 0.906148 

新しいテキストまたはxlsファイルすべてのyの値が0.1秒単位で取得されるので、実際に時間の値は必要ありません。どんな助けでも大歓迎です。

答えて

1

バッチプロセス:

% Slurp in all lines. 
f = fopen('foo.txt'); 
c = textscan(f,'%s','Delimiter','\n'); 
lines = c{1}; 
fclose(f); 

% Remove headers. 
lines(1:14:end) = []; 
lines(1:13:end) = []; 
lines(1:12:end) = []; 
lines(1:11:end) = []; 

% Extract data. 
output = zeros(numel(lines),2); 
for i = 1:numel(lines) 
    x = lines{i}; 
    output(i,1) = datenum(x(1:19),'mm/dd/yyyy HH:MM:SS') + ... 
     str2double(x(20:26)); 
    output(i,2) = str2double(x(28:end)); 
end 

または状態マシンとして:

f = fopen('foo.txt'); 
output = []; 
while true 
    for i = 1:4 
     x = fgetl(f); 
     if x == -1 
      break 
     end 
    end 
    for i = 1:10 
     x = fgetl(f); 
     if x == -1 
      break 
     end 
     time = datenum(x(1:19),'mm/dd/yyyy HH:MM:SS') + ... 
      str2double(x(20:26)); 
     val = str2double(x(28:end)); 
     output(end+1,1:2) = [time,val]; 
    end 
    if x == -1 
     break 
    end 
end 
fclose(f); 
関連する問題