Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Number of line in a text file
Replies: 12   Last Post: Jan 23, 2013 1:51 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Derek Goring

Posts: 3,892
Registered: 12/7/04
Re: Number of line in a text file
Posted: Jan 23, 2013 1:51 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Thursday, January 24, 2013 7:04:19 AM UTC+13, dpb wrote:
> On 1/23/2013 1:34 AM, leeward.xie@gmail.com wrote:
>

> > On Thursday, November 2, 2006 2:26:12 AM UTC+8, Beno�t Valley wrote:
>
> >> I need to determine the number of line in a text file.
>
> >>
>
> >> Is there a more efficient way that using getl in a while loop ?
>
> >>
>
> >> Thanks,
>
> >> Beno�t
>
> >
>
> > nrows = numel(cell2mat(textscan(fid,'%1c%*[^\n]')))
>
>
>
> If going to do that, would be faster to use a stream form and fread().
>
>
>
> I've not timed it but if going to go text route here's where textread()
>
> would have the advantage of
>
> a) not require separate fopen() and
>
> b) returns an array instead of cell so can skip the cell2mat()
>
>
>
> --


Another solution is to use Perl which is packaged with Matlab:
Put these two lines in a file called countlines.pl
while (<>) {};
print $.,"\n";

Now:
perl 'countlines.pl' 'myfile.txt'
or
nlines=perl('countlines.pl','myfile.txt')




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.