[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
Re: regex-matched formatted input, or selecting a few columns in a CSV file?
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
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');
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.
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.
- Re: regex-matched formatted input, or selecting a few columns in a CSV file?,