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 filefilePath = fullfile(matlabroot,'examples','matlab','sonnets.txt');% Read the filefid = fopen(filePath,'r');str = textscan(fid,'%s','Delimiter','\n');fclose(fid);% Extract first 744 linesstr2 = str{1}(1:744);% Save as a text filefid2 = fopen('test.txt','w');fprintf(fid2,'%s\n', str2{:});fclose(fid2); Related SolutionsMATLAB: How to put a vector in a file [fid,msg] = fopen('filename.txt','wt');assert(fid>=3,msg)fprintf(fid,'%d\n',1:744)fclose(fid);OrV = 1:744;dlmwrite('filename.txt',V(:),'newline','pc') MATLAB: How to put a header in a txt file Straight-forward solution would be like this:% Read the filefid = fopen('yourData.txt','r');str = textscan(fid,'%s','Delimiter','\n');fclose(fid);% Add your headerstr2 = [{'hola!'}; str{1}];% Save as a text filefid2 = fopen('output.txt','w');fprintf(fid2,'%s\n', str2{:});fclose(fid2); Related QuestionI want to delete all the headerlines and delimiters from the data in the below attached text fileImport data into a txt file from matlabTo read a text file using matlabHow to combine two text files having different columns in one file
Best Answer