[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 09:21:48 -0400 |
On Apr 10, 2013, at 9:18 AM, Daniel Arteaga wrote:
> 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
Sorry. In my haste, I didn't notice you'd attached a sample file earlier. The
version below works for me.
# Read text
a = fileread ("test.log");
# Split at EOL
b = regexp (a, '\n', "split");
# Remove five header lines
b(1:5) = [];
# Remove empty lines
b(cellfun (@isempty, b)) = [];
# 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{:}], 4, numel (d));
# Extract the data
Trial = str2num (char (e(1,:)'));
Event_Type = e(2,:);
Code = e(3,:);
Time = str2num (char (e(4,:)'));
Ben