fileread
とstrfind
を以下のように組み合わせることができます。私の経験ではstrfind
はregexp
より使いやすいです。
%% 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
いいえ私はそれらをすべて1つのテキストファイルにコピーできますが、トレースIDを1からNのトレースで始まる連続トレースIDに変更する必要があります。 –