扫描文档,找到一行,提取下面的数字

Scan a document, find a line, extract numbers below

我有以下 .csv 文件:

Marker,"loop_-105"
Id,1

"Time (Seconds)","Sig (ts)"
"1.920576","3.98"
"1.957907","31.58"
"1.9912","34.422"
...

Marker,"loop_-102.1"
Id,1

"Time (Seconds)","Sig (ts)"
"4.920576","3.98"
"2.07","31.58"
"1.9912","34.422"
...

我想打开文件,提取值 -105 和 -102.1 以及两个矩阵中引号中的值。我尝试过 textscan 和 regex,但效果不佳。

str2double(regexp(filetext, '(?<=loop_[^0-9]*)[0-9]*\.?[0-9]+', 'match'));

find(~cellfun(@isempty,strfind(new_measurement,'Time (Seconds)","Sig (ts)')))

这是一次尝试,希望我能正确理解您想要实现的目标。

我使用了以下文件data.csv

Marker,"loop_-105"
Id,1

"Time (Seconds)","Sig (ts)"
"1.920576","3.98"
"1.957907","31.58"
"1.9912","34.422"
"1.920576","3.98"
"1.957907","31.58"
"1.9912","34.422"


Marker,"loop_-102.1"
Id,1

"Time (Seconds)","Sig (ts)"
"4.920576","3.98"
"2.07","31.58"
"1.9912","34.422"

和以下脚本来读取它:

filename = './data.csv';

% read all lines as strings
fid = fopen(filename, 'r');
rawdata = textscan(fid, '%s');
fclose(fid);

% get the single lines
lines = rawdata{1};

% two regexp, one for the Marker, one for the values
startregexp = 'Marker,"loop_(?<startnum>-{0,1}\d+.{0,1}\d*)"';
valueregexp = '"(?<v1>\d+.{0,1}\d*)","(?<v2>\d+.{0,1}\d*)"';

% Data is stored in this cell
result = {};

% Loop through the lines
for lcount = 1:numel(lines)
    start = regexp(lines{lcount}, startregexp, 'names');
    if ~isempty(start)
        % The Marker-Regexp matched, start a new cell entry
        result{end+1} = struct(...
            'Loop', str2double(start.startnum), ...
            'values', []);
        continue
    end

    values = regexp(lines{lcount}, valueregexp, 'names');
    if ~isempty(values)
        % the value regexp matched. Add data 
        result{end}.values(end+1, :) = [str2double(values.v1) str2double(values.v2)];
    end
end

result 是:

>> result

result =

  1×2 cell array

    {1×1 struct}    {1×1 struct}

>> result{1}

ans = 

  struct with fields:

      Loop: -105
    values: [6×2 double]

>> result{1}.values

ans =

    1.9206    3.9800
    1.9579   31.5800
    1.9912   34.4220
    1.9206    3.9800
    1.9579   31.5800
    1.9912   34.4220