One thing you can try is to open the text file as a binary file, find the end of the file and read individual characters (i.e. bytes) back from the end of the file. This code will read the characters from the end of the file until it presses the newline character (ignoring the newline if it finds it at the very end of the file):
fid = fopen('data.txt','r'); %
lastLine = ''; %
offset = 1; %
fseek(fid,-offset,'eof'); %
newChar = fread(fid,1,'*char'); %
while (~strcmp(newChar,char(10))) || (offset == 1)
lastLine = [newChar lastLine]; %
offset = offset+1;
fseek(fid,-offset,'eof'); %
newChar = fread(fid,1,'*char'); %
end
fclose(fid); %
source
share