MATLAB文本文件中两个字符串之间更改多个数字

3
我有几个GPS文件,需要将它们合并成一个文件。因此,我需要更改一个条目,因为所有条目都重新从跟踪编号1开始。
以下是文本文件的示例:
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 #1 at position 6.000000 $GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A
Trace #2 at position 7.000000 $GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A
Trace #3 at position 8.000000 $GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,M,,,,*0A
我的期望输出应该每个“跟踪号”都更改为递增的(从“跟踪 #1”到“跟踪 #5”)。位置不必更改,因为它不用于进一步处理。
总共有大约18000个跟踪。

不,我可以将它们全部复制到一个文本文件中,但必须将跟踪 ID 更改为连续的跟踪 ID,从 1 到 N 条跟踪。 - Jack Jones
2个回答

2

虽然在合并所有文件时纠正计数器是可能的,但我认为对于您的情况,在最终合并后使用一些后处理更容易。也就是说,先合并所有文件,然后纠正最终文件。

以下是我的后处理解决方案。简单来说:

  1. 将所有文本读入内存
  2. 搜索要更正的行
  3. 将这些行分成需要保留和需要更正的部分
  4. 对相关部分进行更正
  5. 将所有内容写入文件。

请注意,regexpstrfind慢且稍微难用,但它使您能够更加灵活地处理格式。例如,不同输出文件之间的意外前导空格、不同数量的空格等愚蠢的事情根本不会影响处理。此外,regexp允许您在一次调用中完成步骤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,但这样做也无妨。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接