3

我将数据存储为文本文件,如下所示:

1377303313.298551: 1377303312.800000, **40.497522, -74.434818, 0.026000,** PS  

1377303313.320448: R255 1

1377303313.369429: R255 1 

1377303313.419430: R255 1 

1377303313.468416: R255 1

1377303313.676656: 1377303313.200000, **40.497521, -74.434819, 0.010000** PS

1377303313.715420: R255 1  

1377303313.814361: R255 1

我希望我的文件看起来像这样:

1 255 **40.497522  -74.434818   0.026000** 1377303313.320448

1 255 **40.497522  -74.434818  0.026000** 1377303313.369429

1 255 **40.497522  -74.434818   0.026000** 1377303313.419430

1 255 **40.497522  -74.434818   0.026000** 1377303313.468416

1 255 **40.497521  -74.434819   0.010000** 1377303313.715420

1 255 **40.497521  -74.434819   0.010000** 1377303313.814361

简而言之,我需要将粗体部分延续到线条上,直到它们达到下一个 PS 测量值,然后不断重复相同的操作。我真的很努力解决这个问题,我会很感激一些帮助!

提前致谢。

4

1 回答 1

1

我很快整理了一些脚本。我假设您的输入文件具有固定格式,从某种意义上说,值的长度不会在行之间发生变化。这样可以更容易地从线条中提取您需要的东西,例如“粗体部分”。如果它们不固定,则提取可能需要正则表达式,但脚本的一般结构不应改变。

disp(' ');

fname = 'data.txt';

% desired output line format
desiredlineTemplate = '1 255 %s %s\n';


boldPart = '';

fid = fopen(fname);

% get first line
tline = fgets(fid);

if  numel(tline) > 36 
    % check if first line in a file is a PS line
    % based on its length.
    % lines with PS are much longer than the lines without PS
    % so we can use this information

    % extract the "bold part"
    boldPart = tline(39:end-4);

    % remove commas
    boldPart = strrep(boldPart,',','');
end

while ischar(tline)

    % disp(tline);

    if numel(tline) == 1,   
        % enmpty lines have length of 1. At least 
        % when I tried this. If this is not the case for your 
        % text file, u can ignor this bit.
        tline = fgets(fid); 
        continue;
    end


    % lines with PS are much longer than the lines without PS
    % so we can use this information
    if  numel(tline) > 36 
        % this is a line with PS
        % so extract "bold part"
        boldPart = tline(39:end-4);

        % remove comma
        boldPart = strrep(boldPart,',','');

    else
        % now we are in a line without PS. Thus need to reformat this line
        % and add "bold part" extracted earlier.

        % extract the first number in a line
        % again based on ints length
        firstNumber = tline(1:17);

        % CONSTRUCT THE DISIRED LINE OUTPUT
        desiredLine = sprintf(desiredlineTemplate, boldPart, firstNumber);

        disp(desiredLine);
    end

    tline = fgets(fid); 

end

fclose(fid); 

输出是:

>>  
1 255 40.497522 -74.434818 0.026000 1377303313.320448

1 255 40.497522 -74.434818 0.026000 1377303313.369429

1 255 40.497522 -74.434818 0.026000 1377303313.419430

1 255 40.497522 -74.434818 0.026000 1377303313.468416

1 255 40.497521 -74.434819 0.010000 1377303313.715420

1 255 40.497521 -74.434819 0.010000 1377303313.814361
于 2013-08-29T00:22:03.163 回答