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:
read specific line from text file

Subject: read specific line from text file

From: DitzaN

Date: 1 Dec, 2010 12:59:35

Message: 1 of 5

Hi,

I have csv file in this format

5
56 65 6
6
46 65
ABC

I need only specific line of the file.

I now I can use this code
fid=fopen('filename');
C = textscan(f, '%s')
RelevantLine=C{1}{LineNum};

Is there is why to read only the line I need?

thanks,
Ditza

Subject: read specific line from text file

From: Aurelien Queffurust

Date: 1 Dec, 2010 13:23:05

Message: 2 of 5

DitzaN <ditzaspam@gmail.com> wrote in message <1375586088.34682.1291208405543.JavaMail.root@gallium.mathforum.org>...
> Hi,
>
> I have csv file in this format
>
> 5
> 56 65 6
> 6
> 46 65
> ABC
>
> I need only specific line of the file.
>
> I now I can use this code
> fid=fopen('filename');
> C = textscan(f, '%s')
> RelevantLine=C{1}{LineNum};
>
> Is there is why to read only the line I need?
>
> thanks,
> Ditza

Subject: read specific line from text file

From: John

Date: 1 Dec, 2010 15:02:22

Message: 3 of 5

DitzaN <ditzaspam@gmail.com> wrote in message <1375586088.34682.1291208405543.JavaMail.root@gallium.mathforum.org>...
> Hi,
>
> I have csv file in this format
>
> 5
> 56 65 6
> 6
> 46 65
> ABC
>
> I need only specific line of the file.
>
> I now I can use this code
> fid=fopen('filename');
> C = textscan(f, '%s')
> RelevantLine=C{1}{LineNum};
>
> Is there is why to read only the line I need?
>
> thanks,
> Ditza

Hi,

Use following line:
C = textscan(fid, '%s','delimiter', '\n');

> RelevantLine=C{1}{LineNum};
Yes, it gives you the line you want to read.

You can also use textread:
C = textread('filename', '%s','delimiter', '\n');

In this case you get your line using:
C{LineNum};

Regards.

Subject: read specific line from text file

From: Kelly Kearney

Date: 1 Dec, 2010 15:34:07

Message: 4 of 5

DitzaN <ditzaspam@gmail.com> wrote in message <1375586088.34682.1291208405543.JavaMail.root@gallium.mathforum.org>...
> Hi,
>
> I have csv file in this format
>
> 5
> 56 65 6
> 6
> 46 65
> ABC
>
> I need only specific line of the file.
>
> I now I can use this code
> fid=fopen('filename');
> C = textscan(f, '%s')
> RelevantLine=C{1}{LineNum};
>
> Is there is why to read only the line I need?
>


Try this:

linenum = 3; % Or whichever line you wish to read
textscan(fid, '%s', 1, 'delimiter', '\n', 'headerlines', linenum-1);

-Kelly

Subject: read specific line from text file

From: DitzaN

Date: 5 Dec, 2010 10:11:25

Message: 5 of 5

Thanks

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