Is there a way in Matlab to determine the number of lines in a file without looping through each line?
问题 Obviously one could loop through a file using fgetl or similar function and increment a counter, but is there a way to determine the number of lines in a file without doing such a loop? 回答1: I like to use the following code for exactly this task fid = fopen('someTextFile.txt', 'rb'); %# Get file size. fseek(fid, 0, 'eof'); fileSize = ftell(fid); frewind(fid); %# Read the whole file. data = fread(fid, fileSize, 'uint8'); %# Count number of line-feeds and increase by one. numLines = sum(data ==