Read fields from text file and store them in a structure

我怕爱的太早我们不能终老 提交于 2019-12-11 19:38:37

问题


I am trying to read a file that looks as 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 I have this code:

fid1= fopen('chb01-summary.txt')
data=struct('id',{},'stime',{},'etime',{},'seizenum',{},'sseize',{},'eseize',{});
if fid1 ==-1
    error('File cannot be opened ')
end
tline= fgetl(fid1);
while ischar(tline)
    i=1;
    disp(tline);
end

I want to use regexp to find the expressions and so I 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 a loop to find the line at which file name starts with:

for (firstline<1) || (firstline>1 )
    firstline= strfind(tline, 'File Name') 
    tline=fgetl(fid1);
end 

and now I'm stumped.

Suppose that I am at the line at which the information is there, how do I store the information with regexp? I got an empty array for data after running the code once...

Thanks in advance.


回答1:


I find it the easiest to read the lines into a cell array first using textscan:

%// Read lines as strings
fid = fopen('input.txt', 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);

and then apply regexp on it to do the rest of the manipulations:

%// Parse field names and values
C = regexp(C{:}, '^\s*([^:]+)\s*:\s*(.+)\s*', 'tokens');
C = [C{:}];                          %// Flatten the cell array
C = reshape([C{:}], 2, []);          %// Reshape into name-value pairs

Now you have a cell array C of field names and their corresponding (string) values, and all you have to do is plug it into struct in the correct syntax (using a comma-separated list in this case). Note that the field names have spaces in them, so this needs to be taken care of before they can be used (e.g replace them with underscores):

C(1, :) = strrep(C(1, :), ' ', '_'); %// Replace spaces with underscores
data = struct(C{:});

Here's what I get for your 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, it is possible to prettify it even more by converting all relevant numbers to numerical values, grouping the 'channel' fields together and such, but I'll leave this to you. Good luck!



来源:https://stackoverflow.com/questions/17818267/read-fields-from-text-file-and-store-them-in-a-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!