MATLAB: How to remove some lines of a file

deletefilelinetext filetxt

I have a File of 8.760 lines. I'll use just the first 744 lines. How can I delete the others?

Best Answer

Assuming your file is a text file, you can extract the first 744 lines ans save it like this:
% Full path of the sample text file
filePath = fullfile(matlabroot,'examples','matlab','sonnets.txt');
% Read the file
fid = fopen(filePath,'r');
str = textscan(fid,'%s','Delimiter','\n');
fclose(fid);
% Extract first 744 lines
str2 = str{1}(1:744);
% Save as a text file
fid2 = fopen('test.txt','w');
fprintf(fid2,'%s\n', str2{:});
fclose(fid2);