MATLAB: Write last line of .txt file

text filetext;textscan

Hi everyone, I just need to save only the last value of the second column of my .txt file. Besides I need to obtain also the last value of the second column for another .txt file which has the same structure but can have more or less rows of the previous one.

Best Answer

You have two options load all the file and read the content you want.
filedata = importdata('mydata.txt') ;
data = filedata.data ;
iwant = data(end,2)
Second option is read file line by line and store the end value. As of now you proceed with the first option. To read file line by line, this is the way to proceed:
fid = fopen('mydata.txt');
tline = fgets(fid);
while ischar(tline)
disp(tline)
tline = fgets(fid);
end
fclose(fid);