2016-05-03 3 views
3

私はいくつかのGPSファイルを持っており、それらを1つのファイルにマージしたいと思います。したがって、1つのエントリを変更する必要があります。これは、すべてがトレース#1で開始されるからです。 ,,位置0.000000 $ GPGGA、092105.95,4635.2492567、N、00823.5402932、E、1,13,0.8,2355.019、MでMATLABのテキストファイルは2つの文字列の間でいくつかのエントリ(数値)を変更します

トレース#1:

はここで、テキストファイルの例です。 ,, *位置1.000000 $ GPLLQ、092106.10,042916 ,,,,, 0,13,5.522 00

トレース#2 ,, * 5D $ GPGGA、092106.20,4635.2492568、N、00823.5402891、E、1 、13,0.8,2355.020、M ,,,, * 00

位置6.000000のトレース#1 $ GPGGA位置で、092106.70,4635.2492591、N、00823.5402862、E、1,13,0.8,2355.034、M ,,,, * 0A

トレース#2 7.000000 $ GPGGA、092106.70,4635.2492591、N、00823.5402862、E、 1,13,0.8,2355.034、M ,,,, * 0A

位置8.000000 $ GPGGA、092106.70,4635.2492591、N、00823.5402862、E、1,13,0.8,2355.034、Mのトレース#3 ,, ,, * 0A

私の所望の出力は、すべてのTrace #増分に変更(Trace #5からTrace #1)を持っている必要があります。この位置は、それ以上の処理には使用されないため、変更する必要はありません。

合計で約18000のトレースがあります。

+0

いいえ私はそれらをすべて1つのテキストファイルにコピーできますが、トレースIDを1からNのトレースで始まる連続トレースIDに変更する必要があります。 –

答えて

1

filereadstrfindを以下のように組み合わせることができます。私の経験ではstrfindregexpより使いやすいです。

%% Read file: 
file_string = fileread('C:\Users\rfpe\Documents\MATLAB\GPS_data.txt'); 

%% Find indices where the word "Trace" starts 
idx = [strfind(file_string, 'Trace'), numel(file_string)]; 

%% Find the indices where the phrase " at" starts 
idx_2 = strfind(file_string, ' at'); 

%% Loop through the lines of the text, and add each line to 
%% separate cells in new_txt 
for n = 1:numel(idx)-1; 
    new_txt{n} = sprintf('%s%i%s', file_string(idx(n):idx(n)+6), ... 
    n, file_string((idx_2(n)):idx(n+1)-1)); 
end 

%% Open new txt file, with writing rights 
fileID = fopen('GPS_data_new.txt','w'); 

%% Print each cell element into the new text file using fprintf 
fprintf(fileID,'%s', new_txt{:}); 

%% Close the open file: 
fclose(fileID); 

これは、11のトレースを持つファイルに対して、次のように出力します

Trace #1 at position 0.000000 
$GPGGA,092105.95,4635.2492567,N,00823.5402932,E,1,13,0.8,2355.019,M,,,,*00 

Trace #2 at position 1.000000 $GPLLQ,092106.10,042916,,,,,0,13,5.522,,*5D 
$GPGGA,092106.20,4635.2492568,N,00823.5402891,E,1,13,0.8,2355.020,M,,,,*00 

Trace #3 at position 6.000000 
$GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A 

Trace #4 at position 7.000000 
$GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A 

Trace #5 at position 8.000000 
$GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A 

Trace #6 at position 0.000000 
$GPGGA,092105.95,4635.2492567,N,00823.5402932,E,1,13,0.8,2355.019,M,,,,*00 

Trace #7 at position 1.000000 $GPLLQ,092106.10,042916,,,,,0,13,5.522,,*5D 
$GPGGA,092106.20,4635.2492568,N,00823.5402891,E,1,13,0.8,2355.020,M,,,,*00 

Trace #8 at position 6.000000 
$GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A 

Trace #9 at position 7.000000 
$GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A 

Trace #10 at position 8.000000 
$GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A 

Trace #11 at position 0.000000 
$GPGGA,092105.95,4635.2492567,N,00823.5402932,E,1,13,0.8,2355.019,M,,,,*00 
2

すべてのファイルをマージしながら、原理的には、カウンタを補正することが可能であろうが、私はあなたのためだと思います最終的なマージ後にいくつかの後処理を使用する方が簡単です。つまり、すべてのファイルを最初にマージし、最後のファイルを修正します。

以下は私の後処理ソリューションです。言葉では:

  1. は、関連部分
  2. に修正を行う
  3. を修正するために維持し、部品までの部分にこれらの行を分割
  4. を修正すべきラインのためのメモリ
  5. 検索にすべてのテキストを読みますすべてをファイルに書き出します。 regexpが遅く、strfindよりも使用するように少し難しいですが、それはあなたが、フォーマットでより柔軟にすることができますことを

注意。間違った先頭スペースや異なる出力ファイル間のスペースなどのような愚かなものは、処理にまったく影響しません。また、regexpでは、1回のコールで2と3のステップを実行することができ、合計でstrfindよりもずっと速く簡単になります。

はとにかく、ここに行く:まあしかし、

% Read the file into memory 
fid = fopen('GPS_data.txt', 'r'); 
    txt = textscan(fid, '%s', 'delimiter','\n'); 
    txt = txt{1}; 
fclose(fid); 

% Find relevant lines and separate into tokens 
tok = regexp(txt, ... 
      '^(\s*Trace\s*#\s*)(\d*)(.*)$',... 
      'tokens'); 

% Do the replacements 
matches = ~cellfun('isempty', tok); 
txt(matches) = cellfun(@(x,y)[x{1}{1} int2str(y) x{1}{3}], ...  
         tok(matches),... 
         num2cell(1:nnz(matches))',... 
         'UniformOutput', false); 

% Write results to file     
fid = fopen('GPS_data_corrected.txt', 'w'); 
    fprintf(fid, '%s\n', txt{:}); 
fclose(fid); 

arrayfunを使用するか、プレーンなループはあなたがnum2cellを取り除くことができます。

関連する問題