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:
count number of rows in a file

Subject: count number of rows in a file

From: Mike Liu

Date: 15 Oct, 2010 05:55:04

Message: 1 of 3

Hi
  How to obtain the number of rows in a text file ?

file looks like :
 1 2006/07/19 02:22:12 5 1 3 11
 2 2006/07/29 02:29:55 5 1 1 16
 3 2006/08/19 02:26:03 5 1 1 3
...

I'd like to read and save it. However, I need to create empty array with known number of data. I use while(~feof()) ... fgetl, but I am thinking if there is a better one.

Mike

Subject: count number of rows in a file

From: Grzegorz Knor

Date: 15 Oct, 2010 06:26:03

Message: 2 of 3

"Mike Liu" <sulfateion@gmail.com> wrote in message <i98qbo$b1e$1@fred.mathworks.com>...
> Hi
> How to obtain the number of rows in a text file ?
>
> file looks like :
> 1 2006/07/19 02:22:12 5 1 3 11
> 2 2006/07/29 02:29:55 5 1 1 16
> 3 2006/08/19 02:26:03 5 1 1 3
> ...
>
> I'd like to read and save it. However, I need to create empty array with known number of data. I use while(~feof()) ... fgetl, but I am thinking if there is a better one.
>
> Mike


On unix system you can use wc command:

[nol nol] = system('wc -l your_file.TXT');
nol = str2num(strtok(nol));

In general case:

fid = fopen('your_file.TXT','r');
fseek(fid, 0, 'eof');
chunksize = ftell(fid);
fseek(fid, 0, 'bof');
ch = fread(fid, chunksize, '*uchar');
nol = sum(ch == sprintf('\n')); % number of lines
fclose(fid)

regards
Grzegorz

Subject: count number of rows in a file

From: John

Date: 9 Feb, 2011 23:46:04

Message: 3 of 3

works great--thanks!

Tags for 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