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:
how to read a file skipping all the text lines

Subject: how to read a file skipping all the text lines

From: Kushagra Mittal

Date: 30 Jul, 2008 23:58:01

Message: 1 of 8

Hi
I want to read a ASCII file (given below). I only want to
read the numerical data in the form of a matrix and skip all
other text lines and the last character in the file which is ')'

Please suggest me how to do it?

Thanks very much,
Kushagra

----------------------------
File :
----------------------------

(title "Mean Volume fraction (air) ")
(labels "Position" "Mean Volume fraction (air) ")

((xy/key/label "line-14")
-0.0374802 0
-0.0349168 0
-0.032392 0
-0.0297849 0
-0.02719 0
-0.0245914 0
-0.0220529 0
-0.0195992 0
-0.0171695 0
)

Subject: how to read a file skipping all the text lines

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 31 Jul, 2008 06:07:15

Message: 2 of 8

In article <g6qv69$4cu$1@fred.mathworks.com>,
Kushagra Mittal <kmittal@ymail.com> wrote:
>I want to read a ASCII file (given below). I only want to
>read the numerical data in the form of a matrix and skip all
>other text lines and the last character in the file which is ')'

>Please suggest me how to do it?

Use textscan(), telling it that ( and ) are comment characters.
--
  "You may comand nature to the extent only in which you are willing to
  obey her." -- Walter Russell

Subject: how to read a file skipping all the text lines

From: beginner1.mat@hotmail.com

Date: 18 Aug, 2008 23:51:56

Message: 3 of 8

On Jul 30, 11:07=A0pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <g6qv69$4c...@fred.mathworks.com>,
>
> Kushagra Mittal <kmit...@ymail.com> wrote:
> >I want to read a ASCII file (given below). I only want to
> >read the numerical data in the form of a matrix andskipall
> >other textlinesand the last character in the file which is ')'
> >Please suggest me how to do it?
>
> Usetextscan(), telling it that ( and ) are comment characters.

Is there a way to have more than one comment character? Say, one of
the lines starts with "Titles" and the other with "Lables". Can you
set commentStyle to skip both of those lines?
fid =3D fopen('data.txt');
Data =3D textscan(fid, '%f%f', 'commentStyle', ['Titles', 'Lables']);%
This doesn't work, but wondering what would???
fclose(fid);
Thanks

Subject: how to read a file skipping all the text lines

From: Paul

Date: 19 Aug, 2008 00:17:03

Message: 4 of 8

beginner1.mat@hotmail.com wrote in message
<94268f3f-48b4-45af-b762-be0eda8c3364@x16g2000prn.googlegroups.com>...
> On Jul 30, 11:07=A0pm, rober...@ibd.nrc-cnrc.gc.ca (Walter
Roberson)
> wrote:
> > In article <g6qv69$4c...@fred.mathworks.com>,
> >
> > Kushagra Mittal <kmit...@ymail.com> wrote:
> > >I want to read a ASCII file (given below). I only want to
> > >read the numerical data in the form of a matrix andskipall
> > >other textlinesand the last character in the file which
is ')'
> > >Please suggest me how to do it?
> >
> > Usetextscan(), telling it that ( and ) are comment
characters.
>
> Is there a way to have more than one comment character?
Say, one of
> the lines starts with "Titles" and the other with
"Lables". Can you
> set commentStyle to skip both of those lines?
> fid =3D fopen('data.txt');
> Data =3D textscan(fid, '%f%f', 'commentStyle', ['Titles',
'Lables']);%
> This doesn't work, but wondering what would???
> fclose(fid);
> Thanks

Try putting Titles and Lables in a cell array and see if
that works.

Subject: how to read a file skipping all the text lines

From: beginner1.mat@hotmail.com

Date: 19 Aug, 2008 02:03:29

Message: 5 of 8

On Aug 18, 5:17=A0pm, "Paul " <p...@ceri.memphis.edu> wrote:
> beginner1....@hotmail.com wrote in message
>
> <94268f3f-48b4-45af-b762-be0eda8c3...@x16g2000prn.googlegroups.com>...
>
>
>
> > On Jul 30, 11:07=3DA0pm, rober...@ibd.nrc-cnrc.gc.ca (Walter
> Roberson)
> > wrote:
> > > In article <g6qv69$4c...@fred.mathworks.com>,
>
> > > Kushagra Mittal <kmit...@ymail.com> wrote:
> > > >I want to read a ASCII file (given below). I only want to
> > > >read the numerical data in the form of a matrix andskipall
> > > >other textlinesand the last character in the file which
> is ')'
> > > >Please suggest me how to do it?
>
> > > Usetextscan(), telling it that ( and ) are comment
> characters.
>
> > Is there a way to have more than one comment character?
> Say, one of
> > the lines starts with "Titles" and the other with
> "Lables". =A0Can you
> > set commentStyle to skip both of those lines?
> > fid =3D3D fopen('data.txt');
> > Data =3D3D textscan(fid, '%f%f', 'commentStyle', ['Titles',
> 'Lables']);%
> > This doesn't work, but wondering what would???
> > fclose(fid);
> > Thanks
>
> Try putting Titles and Lables in a cell array and see if
> that works.

It didn't...seems like that is used for start and stop when put into a
cell array.

Subject: how to read a file skipping all the text lines

From: Miroslav Balda

Date: 19 Aug, 2008 04:37:02

Message: 6 of 8

"Kushagra Mittal" <kmittal@ymail.com> wrote in message
<g6qv69$4cu$1@fred.mathworks.com>...
> Hi
> I want to read a ASCII file (given below). I only want to
> read the numerical data in the form of a matrix and skip all
> other text lines and the last character in the file which
is ')'
>
> Please suggest me how to do it?
>
> Thanks very much,
> Kushagra
>
> ----------------------------
> File :
> ----------------------------
>
> (title "Mean Volume fraction (air) ")
> (labels "Position" "Mean Volume fraction (air) ")
>
> ((xy/key/label "line-14")
> -0.0374802 0
> -0.0349168 0
> -0.032392 0
> -0.0297849 0
> -0.02719 0
> -0.0245914 0
> -0.0220529 0
> -0.0195992 0
> -0.0171695 0
> )
>

Hi,

I think that your file is good to be read by the function
txt2mat from File Exchange, Id 18430.

Mira

Subject: how to read a file skipping all the text lines

From: Paul

Date: 19 Aug, 2008 04:37:02

Message: 7 of 8

beginner1.mat@hotmail.com wrote in message
<7f58cd55-3107-4b4e-8b5b-bee2840f17d5@r15g2000prh.googlegroups.com>...
> On Aug 18, 5:17=A0pm, "Paul " <p...@ceri.memphis.edu> wrote:
> > beginner1....@hotmail.com wrote in message
> >
> >
<94268f3f-48b4-45af-b762-be0eda8c3...@x16g2000prn.googlegroups.com>...
> >
> >
> >
> > > On Jul 30, 11:07=3DA0pm, rober...@ibd.nrc-cnrc.gc.ca
(Walter
> > Roberson)
> > > wrote:
> > > > In article <g6qv69$4c...@fred.mathworks.com>,
> >
> > > > Kushagra Mittal <kmit...@ymail.com> wrote:
> > > > >I want to read a ASCII file (given below). I only
want to
> > > > >read the numerical data in the form of a matrix
andskipall
> > > > >other textlinesand the last character in the file which
> > is ')'
> > > > >Please suggest me how to do it?
> >
> > > > Usetextscan(), telling it that ( and ) are comment
> > characters.
> >
> > > Is there a way to have more than one comment character?
> > Say, one of
> > > the lines starts with "Titles" and the other with
> > "Lables". =A0Can you
> > > set commentStyle to skip both of those lines?
> > > fid =3D3D fopen('data.txt');
> > > Data =3D3D textscan(fid, '%f%f', 'commentStyle',
['Titles',
> > 'Lables']);%
> > > This doesn't work, but wondering what would???
> > > fclose(fid);
> > > Thanks
> >
> > Try putting Titles and Lables in a cell array and see if
> > that works.
>
> It didn't...seems like that is used for start and stop
when put into a
> cell array.


Sorry about that.

What about declaring the '(' as a commentStyle and the ')'
as an endOfLine character?

Subject: how to read a file skipping all the text lines

From: Andres

Date: 19 Aug, 2008 07:09:02

Message: 8 of 8

"Miroslav Balda" <balda.nospam@cdm.it.cas.cz> wrote in
message <g8dile$doh$1@fred.mathworks.com>...
> "Kushagra Mittal" <kmittal@ymail.com> wrote in message
> <g6qv69$4cu$1@fred.mathworks.com>...
> > Hi
> > I want to read a ASCII file (given below). I only want
to
> > read the numerical data in the form of a matrix and
skip all
> > other text lines and the last character in the file
which
> is ')'
> >
> > Please suggest me how to do it?
> >
> > Thanks very much,
> > Kushagra
> >
> > ----------------------------
> > File :
> > ----------------------------
> >
> > (title "Mean Volume fraction (air) ")
> > (labels "Position" "Mean Volume fraction (air) ")
> >
> > ((xy/key/label "line-14")
> > -0.0374802 0
> > -0.0349168 0
> > -0.032392 0
> > -0.0297849 0
> > -0.02719 0
> > -0.0245914 0
> > -0.0220529 0
> > -0.0195992 0
> > -0.0171695 0
> > )
> >
>
> Hi,
>
> I think that your file is good to be read by the function
> txt2mat from File Exchange, Id 18430.
>
> Mira

I agree. One of the txt2mat-solutions would be to use
the 'BadLineString' argument to ignore all lines containing
a ')' :

filename = 'C:\myfile.txt';
data = txt2mat(filename,'BadLineString',{')'});

Best regards
Andres

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