Home > ctdcal > count_lines.m

count_lines

PURPOSE ^

count_lines: Count the number of lines in an ascii file.

SYNOPSIS ^

function lineno = count_lines(fname)

DESCRIPTION ^

 count_lines: Count the number of lines in an ascii file.
 
 CTD Calibration toolbox
 
 INPUT: 
   filename: path and name of file to count    

 OUTPUT:
   count: the number of lines in the file.
                 Returns an empty array if the file or can't be opened, or zero if it is empty.

 DESCRIPTION:    
 Counts the number of lines in an ascii file.  Should work on both windows and
 linux machines.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function lineno  = count_lines(fname)
0002 % count_lines: Count the number of lines in an ascii file.
0003 %
0004 % CTD Calibration toolbox
0005 %
0006 % INPUT:
0007 %   filename: path and name of file to count
0008 %
0009 % OUTPUT:
0010 %   count: the number of lines in the file.
0011 %                 Returns an empty array if the file or can't be opened, or zero if it is empty.
0012 %
0013 % DESCRIPTION:
0014 % Counts the number of lines in an ascii file.  Should work on both windows and
0015 % linux machines.
0016 %
0017 %
0018 
0019 %
0020 % AUTHOR:
0021 %            Derrick Snowden
0022 %            NOAA/AOML/PhOD
0023 %            Mon Oct 04 12:27:43 EDT 2004
0024 % This is based on file2cell.m by P. Acklam.
0025 %
0026 % CHANGELOG:
0027 %   08-Jul-2004, Version 1.0
0028 %        * Initial version.
0029 %
0030 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0031 
0032 % See of file exists.
0033 if ~exist(fname, 'file')
0034   error([fname ': No such file.']);
0035 end
0036 
0037 % Try to open file for reading.
0038 fid = fopen(fname, 'r');
0039 if fid < 0
0040   warning([fname ': Can''t open file for reading.']);
0041   lineno = [];
0042   return
0043 end
0044 
0045 % Initialize line counter.
0046 lineno = 0;
0047 
0048 % Read data from file.
0049 while ~feof(fid)
0050     tmp = fgetl(fid);
0051     if tmp >= 0
0052           lineno = lineno + 1;              % increment line counter
0053     else
0054         break
0055     end
0056 end
0057 
0058 % Close file
0059 fclose(fid);
0060 
0061

Generated on Fri 08-Oct-2004 11:57:17 by m2html © 2003