coder741
coder741

Reputation: 17

Extract Specific Line Number in text file with MATLAB

I have a file that is nth lines long and I want to extract line 10 from the file and read it in as a string. I don't want to import the file, I don't want to search for a string in the file, and I don't want to skip nth lines, I just want to read in line 10. I'm having trouble scripting this up, how can I do this?

fileID = fopen(test.txt','r');


fclose(fileID)

Upvotes: 0

Views: 731

Answers (2)

blindpanic
blindpanic

Reputation: 16

Matlab can't find the nth line without linearly searching for eol characters. Even if a function did exist to go to line 10, that function would still need to read every line and check for eol. You have to either skip n lines use fgets/fgetl or use fseek if you know how many bytes precede the line.

Upvotes: 0

Kon
Kon

Reputation: 4099

If you knew exactly how many bytes into the file line 10 was, you could use fseek to skip to that offset in file. If you do not know this, then you have no other option than to read line by line using fgetl and ignore lines until you get to line 10.

Upvotes: 1

Related Questions