Thread Subject: "reading data from text file"

Subject: "reading data from text file"

From: Hosein

Date: 16 May, 2008 17:34:02

Message: 1 of 11

I have a text file as follows:

CC
CC
*----
    2 3
34 6
CC
*---
2.2 3.3 4.4

how can I read just numbers? or how can I ask matlab to
ignore all lines started by 'CC' and read data after lines
started by '*'? (Also, what about spaces?)
I want to read each line individually.
if anybody can write some lines of a code to read even above
example, I will appreciate.
(I already read the example regarding to 'textread' and
'textscan'. but I did not find anything clear.)

Subject: Re:

From: Andres Toennesmann

Date: 16 May, 2008 18:49:02

Message: 2 of 11

"Hosein " <Kalaeimh@yahoo.com> wrote in message
<g0kgia$nlu$1@fred.mathworks.com>...
> I have a text file as follows:
>
> CC
> CC
> *----
> 2 3
> 34 6
> CC
> *---
> 2.2 3.3 4.4
>
> [...]
> if anybody can write some lines of a code to read even above
> example, I will appreciate.
> (I already read the example regarding to 'textread' and
> 'textscan'. but I did not find anything clear.)

A = txt2mat('C:\myfile.txt',0,-3,'BadLineString',{'*','C'});

is a possible one-liner solution. txt2mat can be found on
the file exchange (see its help for details).

Hth,
regards
Andres

Subject: Re:

From: Hosein

Date: 16 May, 2008 19:21:02

Message: 3 of 11

"Andres Toennesmann" <rantore@werb.de> wrote in message
<g0kkuu$47v$1@fred.mathworks.com>...
> "Hosein " <Kalaeimh@yahoo.com> wrote in message
> <g0kgia$nlu$1@fred.mathworks.com>...
> > I have a text file as follows:
> >
> > CC
> > CC
> > *----
> > 2 3
> > 34 6
> > CC
> > *---
> > 2.2 3.3 4.4
> >
> > [...]
> > if anybody can write some lines of a code to read even above
> > example, I will appreciate.
> > (I already read the example regarding to 'textread' and
> > 'textscan'. but I did not find anything clear.)
>
> A = txt2mat('C:\myfile.txt',0,-3,'BadLineString',{'*','C'});
>
> is a possible one-liner solution. txt2mat can be found on
> the file exchange (see its help for details).
>
> Hth,
> regards
> Andres


I is not working. There is something wrong with the command

Subject: Reading data

From: Hosein

Date: 16 May, 2008 19:57:01

Message: 4 of 11

"Hosein " <Kalaeimh@yahoo.com> wrote in message
<g0kmqt$k8t$1@fred.mathworks.com>...
> "Andres Toennesmann" <rantore@werb.de> wrote in message
> <g0kkuu$47v$1@fred.mathworks.com>...
> > "Hosein " <Kalaeimh@yahoo.com> wrote in message
> > <g0kgia$nlu$1@fred.mathworks.com>...
> > > I have a text file as follows:
> > >
> > > CC
> > > CC
> > > *----
> > > 2 3
> > > 34 6
> > > CC
> > > *---
> > > 2.2 3.3 4.4
> > >
> > > [...]
> > > if anybody can write some lines of a code to read even
above
> > > example, I will appreciate.
> > > (I already read the example regarding to 'textread' and
> > > 'textscan'. but I did not find anything clear.)
> >
> > A = txt2mat('C:\myfile.txt',0,-3,'BadLineString',{'*','C'});
> >
> > is a possible one-liner solution. txt2mat can be found on
> > the file exchange (see its help for details).
> >
> > Hth,
> > regards
> > Andres
>
>
> I is not working. There is something wrong with the command



would you please 'somebody' help me? I am waiting.
somebody, anybody....

Subject: Reading data

From: someone

Date: 16 May, 2008 20:29:01

Message: 5 of 11

"Hosein " <Kalaeimh@yahoo.com> wrote in message
<g0koud$ikl$1@fred.mathworks.com>...
> "Hosein " <Kalaeimh@yahoo.com> wrote in message
> <g0kmqt$k8t$1@fred.mathworks.com>...
> > "Andres Toennesmann" <rantore@werb.de> wrote in message
> > <g0kkuu$47v$1@fred.mathworks.com>...
> > > "Hosein " <Kalaeimh@yahoo.com> wrote in message
> > > <g0kgia$nlu$1@fred.mathworks.com>...
> > > > I have a text file as follows:
> > > >
> > > > CC
> > > > CC
> > > > *----
> > > > 2 3
> > > > 34 6
> > > > CC
> > > > *---
> > > > 2.2 3.3 4.4
> > > >
> > > > [...]
> > > > if anybody can write some lines of a code to read
even
> above
> > > > example, I will appreciate.
> > > > (I already read the example regarding to 'textread'
and
> > > > 'textscan'. but I did not find anything clear.)
> > >
> > > A = txt2mat('C:\myfile.txt',0,-3,'BadLineString',
{'*','C'});
> > >
> > > is a possible one-liner solution. txt2mat can be
found on
> > > the file exchange (see its help for details).
> > >
> > > Hth,
> > > regards
> > > Andres
> >
> >
> > I is not working. There is something wrong with the
command
>
>
>
> would you please 'somebody' help me? I am waiting.
> somebody, anybody....

How do you know it is not working?

What error message did you get?

It's not easy for us to read your mind!

Subject: Reading data

From: Steven Lord

Date: 16 May, 2008 21:05:40

Message: 6 of 11


"Hosein " <Kalaeimh@yahoo.com> wrote in message
news:g0koud$ikl$1@fred.mathworks.com...
> "Hosein " <Kalaeimh@yahoo.com> wrote in message
> <g0kmqt$k8t$1@fred.mathworks.com>...
>> "Andres Toennesmann" <rantore@werb.de> wrote in message
>> <g0kkuu$47v$1@fred.mathworks.com>...
>> > "Hosein " <Kalaeimh@yahoo.com> wrote in message
>> > <g0kgia$nlu$1@fred.mathworks.com>...

*snip*

>> > A = txt2mat('C:\myfile.txt',0,-3,'BadLineString',{'*','C'});
>> >
>> > is a possible one-liner solution. txt2mat can be found on
>> > the file exchange (see its help for details).
>> >
>> > Hth,
>> > regards
>> > Andres
>>
>> I is not working. There is something wrong with the command
>
> would you please 'somebody' help me? I am waiting.
> somebody, anybody....

Note that Andres indicated that txt2mat is on the file exchange
(http://www.mathworks.com/matlabcentral) -- it is not part of MATLAB itself,
but a user-contributed file. You will need to download it to a work
directory before using it. This is likely the reason it is "not working".

--
Steve Lord
slord@mathworks.com


Subject: Reading data

From: Andres Toennesmann

Date: 17 May, 2008 19:53:01

Message: 7 of 11

"Hosein " <Kalaeimh@yahoo.com> wrote in message
[..]
> >
> > I is not working. There is something wrong with the command
>
>
>
> would you please 'somebody' help me? I am waiting.
> somebody, anybody....

I'm sorry I can't check by myself right now. Maybe a hint to
another solution, using the built-in textscan:
As I'm not sure if textscan supports multiple 'CommentStyle'
parameters (which would have done the job similar to the
multiple 'BadLineString' arguments in txt2mat), you could
write a small funtion that reads your file as a string via
fread, for example, removes those 'CC' and '*----' lines by
strrep "manually" and then applies textscan to the string.
See the respective manual pages please.
Good luck!
Regards
Andres


Subject: Reading data

From: Andres Toennesmann

Date: 18 May, 2008 20:24:01

Message: 8 of 11

> "Hosein " <Kalaeimh@yahoo.com> wrote in message
> [..]
> > >
> > > I is not working. There is something wrong with the
command
> >
> >
> >
> > would you please 'somebody' help me? I am waiting.
> > somebody, anybody....

Just guessing about your problem:
If

A = txt2mat('C:\myfile.txt',0,3,'BadLineString', ...
            {'*','C'}, 'ReadMode','line');

runs properly, it might be an issue with the current fex
version of txt2mat (about translating the version number
string :-( ). An interim fix is on its way to the fex.
If you like, contact me via the fex form, or please provide
some more error details here.
Regards
Andres


Subject: Reading data

From: Hosein

Date: 19 May, 2008 20:45:05

Message: 9 of 11

"Andres Toennesmann" <rantore@werb.de> wrote in message
<g0q391$mdt$1@fred.mathworks.com>...
> > "Hosein " <Kalaeimh@yahoo.com> wrote in message
> > [..]
> > > >
> > > > I is not working. There is something wrong with the
> command
> > >
> > >
> > >
> > > would you please 'somebody' help me? I am waiting.
> > > somebody, anybody....
>
> Just guessing about your problem:
> If
>
> A = txt2mat('C:\myfile.txt',0,3,'BadLineString', ...
> {'*','C'}, 'ReadMode','line');
>
> runs properly, it might be an issue with the current fex
> version of txt2mat (about translating the version number
> string :-( ). An interim fix is on its way to the fex.
> If you like, contact me via the fex form, or please provide
> some more error details here.
> Regards
> Andres
>
>
Thank you very much for your comments. the last comments
solved my problem, however, the waitbar doesn't close until
the end of my code. how can I close it after reading the
data? I tried 'close' with all its version.

Subject: Reading data

From: Andres Toennesmann

Date: 20 May, 2008 07:21:01

Message: 10 of 11

"Hosein " <Kalaeimh@yahoo.com> wrote in message <g0sosh$ls8
$1@fred.mathworks.com>...
> [..] however, the waitbar doesn't close until
> the end of my code. how can I close it after reading the
> data? I tried 'close' with all its version.


The waitbar won't survive a

close all hidden

afaik. But of course, txt2mat should close the waitbar by
itself - and it does so if I let it work on a file with
your demo lines. Could it be that the waitbar was left over
from some previous erroneous call? Everything else would be
a bug and I'd be curious to find out.

Btw, you can also suppress the waitbar (and any command
line output) with the 'Infolevel' argument set to zero,
i.e.:

A = txt2mat('C:\myfile.txt',0,3,'BadLineString', ...
            {'*','C'}, 'ReadMode','line','Infolevel',0);


Regards,
Andres

Subject: Reading data

From: Hosein

Date: 20 May, 2008 15:17:04

Message: 11 of 11

"Andres Toennesmann" <rantore@werb.de> wrote in message
<g0tu4t$bhs$1@fred.mathworks.com>...
> "Hosein " <Kalaeimh@yahoo.com> wrote in message <g0sosh$ls8
> $1@fred.mathworks.com>...
> > [..] however, the waitbar doesn't close until
> > the end of my code. how can I close it after reading the
> > data? I tried 'close' with all its version.
>
>
> The waitbar won't survive a
>
> close all hidden
>
> afaik. But of course, txt2mat should close the waitbar by
> itself - and it does so if I let it work on a file with
> your demo lines. Could it be that the waitbar was left over
> from some previous erroneous call? Everything else would be
> a bug and I'd be curious to find out.
>
> Btw, you can also suppress the waitbar (and any command
> line output) with the 'Infolevel' argument set to zero,
> i.e.:
>
> A = txt2mat('C:\myfile.txt',0,3,'BadLineString', ...
> {'*','C'}, 'ReadMode','line','Infolevel',0);
>
>
> Regards,
> Andres

By this command, the waitbar does not appear even. I want it
to be seen, but after reading data, it is automatically
closed. Probably the reading data is so fast, that is why I
could not see it.

Cheers
hosein

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
textread textscan someone 16 May, 2008 16:30:26
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com