MATLAB文本文件更改两个字符串之间的几个条目(数字)

时间:2016-05-03 07:47:25

标签: matlab

我有几个GPS文件,我想将它们合并为一个文件。因此,我需要更改一个条目,因为所有条目都会再次以#1开始。

以下是文本文件的示例:

  

在位置0.000000处跟踪#1   $ GPGGA,092105.95,4635.2492567,N,00823.5402932,E,1,13,0.8,2355.019,男,,,, * 00

     

在位置1.000000处跟踪#2   $ GPLLQ,092106.10,042916 ,,,,, 0,13,5.522 ,, * 5D   $ GPGGA,092106.20,4635.2492568,N,00823.5402891,E,1,13,0.8,2355.020,男,,,, * 00

     

在位置6.000000处跟踪#1   $ GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,男,,,, * 0A

     

在位置7.000000处跟踪#2   $ GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,男,,,, * 0A

     

在位置8.000000处跟踪#3   $ GPGGA,092106.70,4635.2492591,N,00823.5402862,E,1,13,0.8,2355.034,男,,,, * 0A

我希望的输出应该将每个Trace #更改为增量(Trace #1Trace #5)。该位置不必更改,因为它不用于进一步处理。

总共我有大约18000条痕迹。

2 个答案:

答案 0 :(得分:2)

虽然原则上可以在合并所有文件时纠正计数器,但我认为在最终合并之后使用一些后处理更容易。即,首先合并所有文件,然后更正最终文件。

以下是我的后期处理解决方案。用语言:

  1. 将所有文本读入内存
  2. 搜索要更正的行
  3. 将这些行分成几部分以保持和部分纠正
  4. 对相关部分进行更正
  5. 将所有内容写入文件。
  6. 请注意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 :(得分: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