85524

Read fields from text file and store them in a structure

Question:

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.

Answer1:

I find it the easiest to read the lines into a cell array first using <a href="http://www.mathworks.com/help/matlab/ref/textscan.html" rel="nofollow">textscan</a>:

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

and then apply <a href="http://www.mathworks.com/help/matlab/ref/regexp.html" rel="nofollow">regexp</a> 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 <a href="http://www.mathworks.com/help/matlab/ref/struct.html" rel="nofollow">struct</a> in the correct syntax (using a <a href="http://www.mathworks.com/help/matlab/matlab_prog/comma-separated-lists.html" rel="nofollow">comma-separated list</a> 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 (<em>e.g</em> 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!

Recommend

  • How would I open multiple files, and combine one line of data from each document into a single numbe
  • warning: comparison between pointer and integer in C
  • Word search algorithm using an m.file
  • passing a 2D array to a function
  • Use regexp in Matlab to return the value of a variable from a text file
  • Matlab Questions about Audioplayer GUI
  • Fibonacci numbers using matlab [duplicate]
  • How to click on a coordinate on a screen?
  • Unit Test case using Rspec for each loop
  • What part of this RegEx is causing it to find no matches in IE7 and IE8?
  • Make mongoid session read only
  • Matlab to Python Conversion binary file read
  • Removing the last digits in string
  • How to get all use statements declared in PHP class file
  • javascript regexp - replace all floating point numbers in a string with rounded numbers
  • Weighted round robin dns between 2 Cloudfront distributions
  • Python Paramiko send CTRL+C to an ssh shell
  • Entity Framework unable to delete database, database in use
  • my tic-tac-toe program in matlab does not work [closed]
  • Vuejs: Lifecycle hooks of child routerview components using keep alive
  • ActiveRecord query for a count of new users by day
  • Android fill_parent issue
  • FileReader+canvas image loading problem
  • Pass value from viewmodel to script in zk
  • Using $this when not in object context
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Counter field in MS Access, how to generate?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass