help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regex-matched formatted input, or selecting a few columns in a CSV f


From: mcmcclur
Subject: Re: regex-matched formatted input, or selecting a few columns in a CSV file?
Date: Wed, 16 Jan 2008 18:31:37 -0800

> I am trying to read from a CSV file, where all the numerical
> data is on the 4th and 5th column (1-3rd columns contain
> additional information about instruments). Is there a way to
> read it directly into Octave using something similar to
> fscanf?

One approach is to read in the whole thing and let regexp 
sort it out the mess.  For example,

fid = fopen('testinput.txt');
in = fscanf(fid,'%c');
fclose(fid);
lines = regexp(in,'(^|\n)([^\n]+)', 'match')

The variable lines is now a cellarray, the nth element of
which is a string containing the nth line; returns 
characters (\n) are included at the beginning of each line.

Now you can use cellfun to split these things up further.
For example,

lines2 = cellfun(@(x) regexp(x, '(^|,)[^,]*', 'match'), ...
  lines, 'UniformOutput', false);

These regular expressions certainly need to be modified
a bit, but it might get you close.

Mark McClure



reply via email to

[Prev in Thread] Current Thread [Next in Thread]