For Programmers: Free Programming Magazines  


Home > Archive > Matlab > January 2008 > textread bug or feature?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author textread bug or feature?
Rajiv Narayan

2008-01-30, 11:14 pm

I noticed a possible bug when using TEXTREAD to read comma
delimited files with missing values at the end of a line.
CSVREAD deals with the missing values correctly. However my
particular application reads comma delimited text fields, so
CSVREAD doesnt work.

Here's an example using numeric data though the same applies
to text data:
[color=darkred]

11,12,13,14
21,22,23,
31,32,,
41,42,43,44
[color=darkred]
ans =

11 12 13 14
21 22 23 0
31 32 0 0
41 42 43 44
[color=darkred]
x =

11
12
13
14
21
22
23
31
32
0
41
42
43
44

TEXTREAD skips the the missing values at (2,4) and (3,4),
whereas CSVREAD inserts zeros correctly.

I found that a workaround was to add extra delimiters when
there are missing values at the end of a line. Though this
is not ideal:
[color=darkred]
11,12,13,14
21,22,23,,
31,32,,,
41,42,43,44

[color=darkred]
x =

11
12
13
14
21
22
23
0
31
32
0
0
41
42
43
44


Doug Schwarz

2008-01-30, 11:15 pm

In article <fnr19v$flm$1@fred.mathworks.com>,
"Rajiv Narayan" <askrajiv.nospam@gmail.com> wrote:

> I noticed a possible bug when using TEXTREAD to read comma
> delimited files with missing values at the end of a line.
> CSVREAD deals with the missing values correctly. However my
> particular application reads comma delimited text fields, so
> CSVREAD doesnt work.
>
> Here's an example using numeric data though the same applies
> to text data:
>
>
> 11,12,13,14
> 21,22,23,
> 31,32,,
> 41,42,43,44
>
> ans =
>
> 11 12 13 14
> 21 22 23 0
> 31 32 0 0
> 41 42 43 44
>
> x =
>
> 11
> 12
> 13
> 14
> 21
> 22
> 23
> 31
> 32
> 0
> 41
> 42
> 43
> 44
>
> TEXTREAD skips the the missing values at (2,4) and (3,4),
> whereas CSVREAD inserts zeros correctly.
>
> I found that a workaround was to add extra delimiters when
> there are missing values at the end of a line. Though this
> is not ideal:
>
> 11,12,13,14
> 21,22,23,,
> 31,32,,,
> 41,42,43,44
>
>
> x =
>
> 11
> 12
> 13
> 14
> 21
> 22
> 23
> 0
> 31
> 32
> 0
> 0
> 41
> 42
> 43
> 44



You need to use a format statement that indicates how many items are on
a line. Then you need to use that same number of return variables.
This should do what you want with 4 text items per line:

c = cell(1,4);
[c{:}] = textread('test.csv','%s%s%s%s','delimiter',',');
x = [c{:}];

The result will be a 4x4 cell array.

If you don't know how many items there will be per line, but you can put
an upper limit on it you can just use that. With your test file this
code:

c = cell(1,7);
[c{:}] = textread('test.csv','%s%s%s%s%s%s%s','delimiter',',');
x = [c{:}];

will give you a 4x7 cell array. The last several columns of each row
will contain empty strings.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
Rajiv Narayan

2008-01-31, 10:19 am

Doug Schwarz <see@sig.for.address.edu> wrote in message >
> You need to use a format statement that indicates how many

items are on
> a line. Then you need to use that same number of return

variables.
> This should do what you want with 4 text items per line:
>
> c = cell(1,4);
> [c{:}] = textread('test.csv','%s%s%s%s','delimiter',',');
> x = [c{:}];
>
> The result will be a 4x4 cell array.
>
> If you don't know how many items there will be per line,

but you can put
> an upper limit on it you can just use that. With your

test file this
> code:
>
> c = cell(1,7);
> [c{:}] =

textread('test. csv','%s%s%s%s%s%s%s','delimiter',',');[
color=darkred]
> x = [c{:}];
>
> will give you a 4x7 cell array. The last several columns[/color]
of each row
> will contain empty strings.


Cool, I like your approach better than mine. I could live
with having to specify the number of fields.

I also noticed that the issue only occurs if a format string
is specified
for example textread('test.csv','','delimiter',',') works
well for numeric data and is equivalent to csvread('test.csv').
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com