regex - Read fields from text file and store them in a structure -
i trying read file looks follows:
data sampling rate: 256 hz ************************* channels in edf files: ********************** channel 1: fp1-f7 channel 2: f7-t7 channel 3: t7-p7 channel 4: p7-o1 file name: chb01_02.edf file start time: 12:42:57 file end time: 13:42:57 number of seizures in file: 0 file name: chb01_03.edf file start time: 13:43:04 file end time: 14:43:04 number of seizures in file: 1 seizure start time: 2996 seconds seizure end time: 3036 seconds
so far have code:
fid1= fopen('chb01-summary.txt') data=struct('id',{},'stime',{},'etime',{},'seizenum',{},'sseize',{},'eseize',{}); if fid1 ==-1 error('file cannot opened ') end tline= fgetl(fid1); while ischar(tline) i=1; disp(tline); end
i want use regexp
find expressions , did:
line1 = '(.*\d{2} (\.edf)' data{1} = regexp(tline, line1); tline=fgetl(fid1); time = '^time: .*\d{2]}: \d{2} :\d{2}' ; data{2}= regexp(tline,time); tline=getl(fid1); seizure = '^file: .*\d'; data{4}= regexp(tline,seizure); if data{4}>0 stime = '^time: .*\d{5}'; tline=getl(fid1); data{5}= regexp(tline,seizure); tline= getl(fid1); data{6}= regexp(tline,seizure); end
i tried using loop find line @ file name starts with:
for (firstline<1) || (firstline>1 ) firstline= strfind(tline, 'file name') tline=fgetl(fid1); end
and i'm stumped.
suppose @ line @ information there, how store information regexp
? got empty array data
after running code once...
thanks in advance.
i find easiest read lines cell array first using textscan
:
%// read lines strings fid = fopen('input.txt', 'r'); c = textscan(fid, '%s', 'delimiter', '\n'); fclose(fid);
and apply regexp
on rest of manipulations:
%// parse field names , values c = regexp(c{:}, '^\s*([^:]+)\s*:\s*(.+)\s*', 'tokens'); c = [c{:}]; %// flatten cell array c = reshape([c{:}], 2, []); %// reshape name-value pairs
now have cell array c
of field names , corresponding (string) values, , have plug struct
in correct syntax (using comma-separated list in case). note field names have spaces in them, needs taken care of before can used (e.g replace them underscores):
c(1, :) = strrep(c(1, :), ' ', '_'); %// replace spaces underscores data = struct(c{:});
here's input file:
data = data_sampling_rate: '256 hz' channel_1: 'fp1-f7' channel_2: 'f7-t7' channel_3: 't7-p7' channel_4: 'p7-o1' file_name: 'chb01_03.edf' file_start_time: '13:43:04' file_end_time: '14:43:04' number_of_seizures_in_file: '1' seizure_start_time: '2996 seconds' seizure_end_time: '3036 seconds'
of course, possible prettify more converting relevant numbers numerical values, grouping 'channel' fields , such, i'll leave you. luck!
Comments
Post a Comment