Thread Subject: reading from a text file

Subject: reading from a text file

From: omar

Date: 18 May, 2009 15:03:01

Message: 1 of 9

guys i am making a project and there is a part which involves reading data from 1500 text files, so i have to find a way to automate this process.
the text files look like this:

version: 1
n_points: 20
{
154.05 102.829
223.237 102.829
156.589 175.824
219.429 173.285
129.295 90.7684
177.536 92.0379
199.752 94.5768
247.992 91.4031
125.487 107.272
138.816 105.367
169.284 103.463
209.908 103.463
237.202 106.002
253.07 107.906
188.326 132.661
177.536 140.913
199.752 141.548
187.692 171.381
187.057 190.423
186.422 224.065
}

I have to read the numbers in the two columns such that each two numbers in the same line are read into a 2x1 matrix, which results in 20 matrices(according to the number of lines) each containing two elements.

Any links, explanation, or documentation would be appreciated.
thanks in advance

Subject: reading from a text file

From: omar

Date: 18 May, 2009 15:09:02

Message: 2 of 9

i just wanted to note that i will use the extracted variables to do some calculations so its not a must that each one of them is stored. they can be stored in 20 variables and each time i read the next file the 20 variables are over written with the next 20 variables in the next file.

PLEASE HELP, thanks in advance.

Subject: reading from a text file

From: us

Date: 18 May, 2009 15:15:03

Message: 3 of 9

"omar " <ouzizo@gmail.com> wrote in message <gurtb5$h3$1@fred.mathworks.com>...
> guys i am making a project and there is a part which involves reading data from 1500 text files, so i have to find a way to automate this process.
> the text files look like this:
>
> version: 1
> n_points: 20
> {
> 154.05 102.829
> 223.237 102.829
> 156.589 175.824
> 219.429 173.285
> 129.295 90.7684
> 177.536 92.0379
> 199.752 94.5768
> 247.992 91.4031
> 125.487 107.272
> 138.816 105.367
> 169.284 103.463
> 209.908 103.463
> 237.202 106.002
> 253.07 107.906
> 188.326 132.661
> 177.536 140.913
> 199.752 141.548
> 187.692 171.381
> 187.057 190.423
> 186.422 224.065
> }
>
> I have to read the numbers in the two columns such that each two numbers in the same line are read into a 2x1 matrix, which results in 20 matrices(according to the number of lines) each containing two elements.
>
> Any links, explanation, or documentation would be appreciated.
> thanks in advance

one of the solutions
- given the file's anatomy as shown above

     fnam='foo.txt'; % <- your file name
     s=textread(fnam,'%n','headerlines',3,'whitespace','}');
     s=reshape(s.',2,[]).'
%{
% note: format short g!
     s =
       154.05 102.83
       223.24 102.83
       156.59 175.82
       219.43 173.29
       129.29 90.768
       177.54 92.038
       199.75 94.577
       247.99 91.403
       125.49 107.27
       138.82 105.37
       169.28 103.46
       209.91 103.46
        237.2 106
       253.07 107.91
       188.33 132.66
       177.54 140.91
       199.75 141.55
       187.69 171.38
       187.06 190.42
       186.42 224.07
%}

us

Subject: reading from a text file

From: omar

Date: 18 May, 2009 15:34:02

Message: 4 of 9

man i tried what u wrote me but it resulted in a matrix "s" which contains nothing.

Subject: reading from a text file

From: us

Date: 18 May, 2009 15:45:04

Message: 5 of 9

"omar " <ouzizo@gmail.com> wrote in message <gurv5a$op$1@fred.mathworks.com>...
> man i tried what u wrote me but it resulted in a matrix "s" which contains nothing.

well, MAN, note what i sait:

one of the solutions
- given the file's anatomy as shown above <----- !!!!!

i copied/pasted your example EXACTLY into a file foo.txt - and got the correct result as shown...
the file foo.txt looks like this:

version: 1
n_points: 20
{
154.05 102.829
223.237 102.829
156.589 175.824
219.429 173.285
129.295 90.7684
177.536 92.0379
199.752 94.5768
247.992 91.4031
125.487 107.272
138.816 105.367
169.284 103.463
209.908 103.463
237.202 106.002
253.07 107.906
188.326 132.661
177.536 140.913
199.752 141.548
187.692 171.381
187.057 190.423
186.422 224.065
}

now, if this is NOT the content of your file - then you are wasting CSSMers time...
us

Subject: reading from a text file

From: omar

Date: 18 May, 2009 16:10:03

Message: 6 of 9

"us " <us@neurol.unizh.ch> wrote in message <gurvq0$dai$1@fred.mathworks.com>...
> "omar " <ouzizo@gmail.com> wrote in message <gurv5a$op$1@fred.mathworks.com>...
> > man i tried what u wrote me but it resulted in a matrix "s" which contains nothing.
>
> well, MAN, note what i sait:
>
> one of the solutions
> - given the file's anatomy as shown above <----- !!!!!
>
> i copied/pasted your example EXACTLY into a file foo.txt - and got the correct result as shown...
> the file foo.txt looks like this:
>
> version: 1
> n_points: 20
> {
> 154.05 102.829
> 223.237 102.829
> 156.589 175.824
> 219.429 173.285
> 129.295 90.7684
> 177.536 92.0379
> 199.752 94.5768
> 247.992 91.4031
> 125.487 107.272
> 138.816 105.367
> 169.284 103.463
> 209.908 103.463
> 237.202 106.002
> 253.07 107.906
> 188.326 132.661
> 177.536 140.913
> 199.752 141.548
> 187.692 171.381
> 187.057 190.423
> 186.422 224.065
> }
>
> now, if this is NOT the content of your file - then you are wasting CSSMers time...
> us

thanks man it worked perfectly

Subject: N.B. -- Re: reading from a text file

From: Any one

Date: 18 May, 2009 18:42:15

Message: 7 of 9

us wrote on 18-May-09 08:15 :
[snip]
> fnam='foo.txt'; % <- your file name
> s=textread(fnam,'%n','headerlines',3,'whitespace','}');
[snip]

Help -> Search -> textread:

"Note textread is not recommended. Use textscan to read data from a
text file." -- R2009a help page

Subject: N.B. -- Re: reading from a text file

From: us

Date: 18 May, 2009 23:04:01

Message: 8 of 9

Any one <?O75z!/9?s@WqP0SZo?XAs0NO2&D}CA?1_G.#`}> wrote in message <_Nidnc71fN-aMYzXnZ2dnUVZ_jSdnZ2d@earthlink.com>...
> us wrote on 18-May-09 08:15 :
> [snip]
> > fnam='foo.txt'; % <- your file name
> > s=textread(fnam,'%n','headerlines',3,'whitespace','}');
> [snip]
>
> Help -> Search -> textread:
>
> "Note textread is not recommended. Use textscan to read data from a
> text file." -- R2009a help page

well,
here, we are (for good reasons) still working with r2008b...
also, i do not agree with this help-statement (for very good reasons)...

just a thought
us

Subject: N.B. -- Re: reading from a text file

From: Pekka Kumpulainen

Date: 19 May, 2009 11:11:02

Message: 9 of 9

"us " <us@neurol.unizh.ch> wrote in message <gusph1$nv9$1@fred.mathworks.com>...
> Any one <?O75z!/9?s@WqP0SZo?XAs0NO2&D}CA?1_G.#`}> wrote in message <_Nidnc71fN-aMYzXnZ2dnUVZ_jSdnZ2d@earthlink.com>...
> > us wrote on 18-May-09 08:15 :
> > [snip]
> > > fnam='foo.txt'; % <- your file name
> > > s=textread(fnam,'%n','headerlines',3,'whitespace','}');
> > [snip]
> >
> > Help -> Search -> textread:
> >
> > "Note textread is not recommended. Use textscan to read data from a
> > text file." -- R2009a help page
>
> well,
> here, we are (for good reasons) still working with r2008b...
> also, i do not agree with this help-statement (for very good reasons)...
>
> just a thought
> us

Would you care to share the reasons for disagreeing with this help?
This note has been around at least since 2006a (didn't have older ones at hand)
"Note The textscan function is intended as a replacement for both textread and strread."
Now in 2009a the text has been modified to more strict suggestion. It doesn't say it will be removed in future versions. Yet.
But is textscan has some major disadvantages it would be nice to know. I haven't had any problems with it so far.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
scolding us 18 May, 2009 11:49:04
reshape us 18 May, 2009 11:19:07
textread us 18 May, 2009 11:19:07
code us 18 May, 2009 11:19:07
rssFeed for this Thread

Contact us at files@mathworks.com