Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Is "newline" always a delimiter in strread, textread and textscan?

Subject: Is "newline" always a delimiter in strread, textread and textscan?

From: Philip Nienhuis

Date: 29 Jun, 2011 20:47:34

Message: 1 of 3

Subject says it already. A bit of nitty gritty, admittedly....

The ML docs say for textread, textscan and strread:
default value for delimiter = none, whitespace, one or more whitespace
characters, respectively;
default whitespace = ' \b\t', ' \b\t', '\b\r\n\t' (<= no space!),
respectively.

Now, if I specify a delimiter, is newline always implied, or does the
value/character I assign to delimiter become the only delimiter?

An example:
===========
str = sprintf ('%s\n%s\n%s', '1, 2, 3, 4', '5, , , 8', ....
  ' 9, 10, 11, 12')

c = textscan (str, '%f %d %f %s', 'delimiter', ',')

gives c as (condensed for clarity)
 >> c{:}
ans =
     1
     5
     9
ans =
     2
     0
    10
ans =
     3
   NaN
    11
ans=
    '4'
    '8'
    '12'

... implying the newlines between 4 and 5, and between 8 and 9, are also
delimiters, although I thought I 'd overridden them with the "delimiter"
parameter.

Similar behaviour goes for strread and textread.

So, are delimiters in the call ADDED to the default delimiter collection
or do they REPLACE the default delimiter collection?

Thanks

Subject: Is "newline" always a delimiter in strread, textread and textscan?

From: Philip Borghesani

Date: 1 Jul, 2011 16:02:34

Message: 2 of 3

On 6/29/2011 4:47 PM, Philip Nienhuis wrote:
> Subject says it already. A bit of nitty gritty, admittedly....
>
> The ML docs say for textread, textscan and strread:
> default value for delimiter = none, whitespace, one or more whitespace
> characters, respectively;
> default whitespace = ' \b\t', ' \b\t', '\b\r\n\t' (<= no space!),
> respectively.
>
> Now, if I specify a delimiter, is newline always implied, or does the
> value/character I assign to delimiter become the only delimiter?
>
> An example:
> ===========
> str = sprintf ('%s\n%s\n%s', '1, 2, 3, 4', '5, , , 8', ....
> ' 9, 10, 11, 12')
>
> c = textscan (str, '%f %d %f %s', 'delimiter', ',')
>
... Extra stuff removed
>
> So, are delimiters in the call ADDED to the default delimiter collection
> or do they REPLACE the default delimiter collection?
>
> Thanks

Is this what you want?
  c = textscan (str, '%f %d %f %s', 'delimiter', ...
',','whitespace','','endofline','')

delimiters replace the default delimiter collection.

End of line is treated as a seperate delimiter that can independently be
controlled.

What version of MATLAB are you using? your information seems different
then the current R2011a documentation.

textread and strread are inconsistent and will be removed in the future
to avoid confusion.

Subject: Is "newline" always a delimiter in strread, textread and textscan?

From: Philip Nienhuis

Date: 1 Jul, 2011 18:55:56

Message: 3 of 3

Philip Borghesani wrote:
> On 6/29/2011 4:47 PM, Philip Nienhuis wrote:
>> Subject says it already. A bit of nitty gritty, admittedly....
>>
>> The ML docs say for textread, textscan and strread:
>> default value for delimiter = none, whitespace, one or more whitespace
>> characters, respectively;
>> default whitespace = ' \b\t', ' \b\t', '\b\r\n\t' (<= no space!),
>> respectively.
>>
>> Now, if I specify a delimiter, is newline always implied, or does the
>> value/character I assign to delimiter become the only delimiter?
>>
>> An example:
>> ===========
>> str = sprintf ('%s\n%s\n%s', '1, 2, 3, 4', '5, , , 8', ....
>> ' 9, 10, 11, 12')
>>
>> c = textscan (str, '%f %d %f %s', 'delimiter', ',')
>>
> ... Extra stuff removed
>>
>> So, are delimiters in the call ADDED to the default delimiter collection
>> or do they REPLACE the default delimiter collection?
>>
>> Thanks
>
> Is this what you want?
> c = textscan (str, '%f %d %f %s', 'delimiter', ...
> ',','whitespace','','endofline','')
>
> delimiters replace the default delimiter collection.
>
> End of line is treated as a seperate delimiter that can independently be
> controlled.

You mean by the "endofline" parameter. Well, THAT isn't so clearly
documented (IMO of course.) The docs do not mention how, or for what
purpose, this parameter can be used.

So, do I understand from your example line above that "endofline" =
empty is needed to strip/ignore line endings while reading?

> What version of MATLAB are you using? your information seems different
> then the current R2011a documentation.

(ML2007a; a while back I spotted similar stuff with ML2010a as well.)

My info is taken right from Matlab's on-line docs. Isn't it fair to
assume those comply with the latest ML release?

> textread and strread are inconsistent and will be removed in the future
> to avoid confusion.

IMO the confusion results from inadequate documentation.

Thanks for your answer!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us