Matlab how many lines in a text file




















Sign in to answer this question. Unable to complete the action because of changes made to the page. Reload the page to see its updated state. Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:. Select the China site in Chinese or English for best site performance.

Other MathWorks country sites are not optimized for visits from your location. Toggle Main Navigation. Search Answers Clear Filters. Answers Support MathWorks. Search Support Clear Filters. Support Answers MathWorks. Search MathWorks. MathWorks Answers Support. Edited: Guru on 4 Jul Just out of boredom, here's a function:. Edited: Thanks for comment Walter. The above is off-by-one. An empty file would be counted as 1, a file with one line would count as 2, and so on. I guess this direction is the best that's going to be available.

I wonder if there is a way to avoid the overhead of allocating memory for tline, e. Walter Roberson on 5 Jul Matt, feof never predicts end of file. Because of this, your routine will count 1 for an empty file, and will consistently run 1 high. The arrangement Guru used is proper: test to see if you got end-of-file by looking at the result of an attempt to read. Also keep in mind that in MATLAB, if a routine expects an output and you do not provide a variable or expression to write the output to, then called routine usually allocates the memory for the value within itself and then the memory gets thrown away upon return because it is not needed.

This is not always the case, as routines can test for varargout and potentially not calculate the values that are going to be thrown out, but you should assume that the memory will be allocated and thrown away unless it is documented otherwise. This code does not explicitly store the lines, but as noted above you should assume that each line is being allocated in fgetl and then the result thrown away.

Walter, my version and Guru's return the same result n in tests that I've done. I've also inspected the length of the test file manually and your version runs 1 line too low. However, the documentation coincides with what you are saying about feof, so I cannot immediately reconcile any of this.

In any case, fgetl apparently does not check nargout before allocating memory as you surmised. Too bad, I guess. It seems like an easy enhancement. Oddly, my version is even slower than Guru's. Overhead in feof??? In the text file you used to test with, does the last line end with the line terminator, or does it just end with no terminator? Yes, feof has overhead. I cannot scroll any further than the final line of text. I guess that means it ends with no terminator? Both my version and Guru's correctly count the number of lines of actual text, though.

Walter Roberson on 10 Jan Earlier I wrote that feof never predicts end-of-file. That is true, but I was missing some information about the operation of fgetl and fgets that I just noticed today:. Therefore, these functions sometimes set the end-of-file indicator before they return a value of Vote 3.

I have tested this with files that end with newline and with files that do not end with newline. Jan on 17 Jan I admit, that considering the text mode is strange magic for me.

For counting the lines of my user-defined Matlab files, the C-Mex is 3 times faster than this M-version and gets the same results. Your M-function has the advantages to handle Unicode file names automatically and there is no need to compile an external file. Readers: Currently I assume, that Walter's suggestion satisfies all needs.

If you have to count the lines in thousands of files and need to save time, feel free to contact me. Peter Cook on 5 Oct I have the need for just this function. For a large CSV file I just tested it on x using linecount is only about half as fast Jan on 6 Oct Peter: The Mex function is faster in my tests, but I hesitate to publish it, because I do not know how to handle the exceptions accurately:. If you really have a need for this, I would take the time to fix the code. Peter Cook on 6 Oct I can understand if you don't want to publish it on the file exchange without exception handling, maybe you could email me the c file as-is and I can compile it on my machine?

Elapsed time is 2. Elapsed time is Jan on 9 Oct Peter: fread fptr does read the complete file and stores each byte in a double. Walter Roberson on 9 Oct Boris on 10 Jan Vote 2. I came across this code a while ago which is reasonably fast and works well on large files:. This code has an off-by-one error for files that end in linefeeds.

If you are using OS-X or Linux, try:. You can use! Boris on 17 Jul Or used the code above and check if the file ends in 0A:. Reload the page to see its updated state. Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.

Select the China site in Chinese or English for best site performance. Other MathWorks country sites are not optimized for visits from your location. Toggle Main Navigation. Search Answers Clear Filters. Answers Support MathWorks. Search Support Clear Filters. Support Answers MathWorks. Search MathWorks.



0コメント

  • 1000 / 1000