help-octave
[Top][All Lists]
Advanced

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

Re: Textread from io package


From: Ben Abbott
Subject: Re: Textread from io package
Date: Wed, 10 Apr 2013 08:35:08 -0400

On Apr 10, 2013, at 5:22 AM, Daniel Arteaga wrote:

> Al 09/04/13 15:28, En/na Daniel Arteaga ha escrit:
>> Hi,
>> 
>> The following piece of code works with Matlab:
>> 
>> [Trial Event_Type Code
>> Time]=textread("test.log","%d%s%s%d%*[^\n]","headerlines",5);
>> 
>> In Octave 3.6.2 raises an error (both tested in Ubuntu 12.10 and Windows)
>> 
>> This code should read the first four columns of the attached text file
>> and discard the rest. How can it be adapted to Octave?
>> 
>> I have tried several things like replacing *[^\n] by *, adding and
>> "endofline" option, all without success.
> 
> I have tried to workaround the use of textread with dlmread and other 
> functions without success, because the file to parse
> 
> (i) has a variable number of columns
> (ii) there is a mixture of text and numeric data
> 
> What is the best way to parse these kind of data? (see example in parent post)
> 
> Thanks
> 
> Daniel

This is largely untested.  In particular, the line "e = reshape (...)" may have 
the rows/columns backwards?

# Read text
a = fileread ("test.log")
# Split at EOL
b = regexp (a, "\n", "split");
# Split at white-space or comma (multiple delimiters are collapsed into one)
c = regexp (b", "[\s,]*", "split");
# Only keep the leading 4 entries in each line
d = cellfun (@(c) c(1:4), c, "uniformoutput", false);
# Convert a cell vectors of cells into a cell array
e = reshape ([d{:}], numel (d), 4);
# Extract the data
Trial = cell2mat (e(:,1));
Event_Type = e(:,2);
Code = e(:,3);
Time = cell2mat (e(:,4));

Ben




reply via email to

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