Thread Subject: reading from a file

Subject: reading from a file

From: Johnathan

Date: 12 May, 2009 03:05:04

Message: 1 of 2

if i have a file like

1
2
dog
25
old str1
old str2
old str3
24 25
cat banana
1
2 bears is a lot of bears
3

and so forth, all mixed up, and I want to read the lines that say:
old str1
old str2
old str3

with the intention of obtaining the strings, how do I do that?
I looked at textscan, and it works if the file is not so jumbled, but I can't get it to sort through a messy file. if I say A=textscan(fid,'old %s') I get that
A =
{0x1 cell}

What can I do?

Thanks

Subject: reading from a file

From: John

Date: 13 May, 2009 19:03:02

Message: 2 of 2

Use textread and then extract the items you need by doing pattern search through regexp function -

Say, your file is called list.txt ,

x=textread('list.txt','%s','delimiter','\n')

x =

    '1'
    '2'
    'dog'
    '25'
    'old str1'
    'old str2'
    'old str3'
    '24 25'
    'cat banana'
    '1'
    '2 bears is a lot of bears'
    '3'

mypattern=regexp(x,'^old') %my pattern is also a cell array

mypattern =

    []
    []
    []
    []
    [1]
    [1]
    [1]
    []
    []
    []
    []
    []

j=find(~cellfun('isempty',mypattern))

j =

     5
     6
     7

answer = x(j)
answer=x(j)

answer =

    'old str1'
    'old str2'
    'old str3'

"Johnathan " <durchfalldurchfall@yahoo.com> wrote in message <guap0v$5a9$1@fred.mathworks.com>...
> if i have a file like
>
> 1
> 2
> dog
> 25
> old str1
> old str2
> old str3
> 24 25
> cat banana
> 1
> 2 bears is a lot of bears
> 3
>
> and so forth, all mixed up, and I want to read the lines that say:
> old str1
> old str2
> old str3
>
> with the intention of obtaining the strings, how do I do that?
> I looked at textscan, and it works if the file is not so jumbled, but I can't get it to sort through a messy file. if I say A=textscan(fid,'old %s') I get that
> A =
> {0x1 cell}
>
> What can I do?
>
> Thanks

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 John 13 May, 2009 15:04:06
regexp John 13 May, 2009 15:04:06
textscan Johnathan 11 May, 2009 23:09:02
rssFeed for this Thread

Contact us at files@mathworks.com