help-octave
[Top][All Lists]
Advanced

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

Re: Textread from io package


From: Daniel Arteaga
Subject: Re: Textread from io package
Date: Wed, 10 Apr 2013 15:18:52 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5

Dear Ben,

Al 10/04/13 14:35, En/na Ben Abbott ha escrit:

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));

I get the following error:

warning: unrecognized escape sequence `\s' -- converting to `s'
error: A(I): index out of bounds; value 4 out of bound 1

I'm not familiar enough with regexp to correct myself the code. Anyway, the other similar solution by PetrSt in the thread works well.

Thank you very much for your time and effort,

Daniel



reply via email to

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