Script that Accurately counts lines in a text file

17 ビュー (過去 30 日間)
Matthew Finney
Matthew Finney 2019 年 4 月 2 日
コメント済み: faisal sani 2022 年 8 月 11 日
Would someone be able to help me with a rather simple script I have to write? The script needs to be able to take a text file and count the number of lines of text. Any help is greatly appreciated!
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 2 日
Is newline to be considered a line terminator or a line separator? If it is a line separator then if the last thing in a file is a newline then the emptiness between the newline and the end of file counts as a line.


回答 (2 件)

Guillaume 2019 年 4 月 2 日
linecount = sum(fileread(somefile) == 10) + 1;
See Walter's comment. This will count the emptiness between the newline and end of file as a line if the last line ends in a newline.
Will works both for Windows and Linux line ending (but not old style macs which used char(13) solely as a newline marker).

Image Analyst
Image Analyst 2022 年 8 月 7 日
Try this
textLines = readlines(fileName);
numLines = numel(textlines)
  4 件のコメント
faisal sani
faisal sani 2022 年 8 月 11 日
Okay, thank you, I will post my question above. The information I want to extract is how the object changes the illuminated light when reflected by adding some harmonics to the original sinusoid





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by