MATLAB: How to append three text files together in a new text file #append#textfiles i have just started with matlab and want to know how to read text files and append them together in a new file. Best Answer % REad file1 fid = fopen(file1,'rt') ; % edited as suggested by Stephen CobeldickS1 = textscan(fid,'%s','delimiter','\n') ;S1 = S1{1} ;fclose(fid) ;% Read file2 fid = fopen(file2,'rt') ;S2 = textscan(fid,'%s','delimiter','\n') ;S2 = S2{1} ;fclose(fid) ;% Append both the files S12 = [S1 ; S2] ;% Write to file fid = fopen('data.txt','wt') ;fprintf(fid,'%s\n',S12{:});fclose(fid); Related SolutionsMATLAB: Copy a line from a txt to other txt Hi!count = 1;tline = fgetl(fid01);while ischar(tline)count = count + 1;disp(tline)if count == 3fprintf(fid02,'%s\r\n',tline);endtline = fgetl(fid01);end fclose(fid01);fclose(fid02);You can do as wellfor n = 1:3tline = fgetl(fid01);endfprintf(fid02,'%s\r\n',tline);fclose(fid01);fclose(fid02); MATLAB: Overwriting specific line in a text file and data export file1 = 'FLUX FACTORS.txt' ;file2 = 'HISTORY.txt' ;% REad file1 fid = fopen(file1,'r') ;S1 = textscan(fid,'%s','delimiter','\n') ;S1 = S1{1} ;fclose(fid) ;% REad file2 fid = fopen(file2,'r') ;S2 = textscan(fid,'%s','delimiter','\n') ;S2 = S2{1} ;fclose(fid) ;% Replace 3rd line of file2 with 5th lines of file1S2{3} = S1{5} ;% Write the edited to file fid = fopen('data.txt','wt') ;fprintf(fid,'%s\n',S2{:});fclose(fid); Related QuestionUsing a function strncmpHow to read multiple file .txt by using readtableAm I having trouble reading an ascii file in notepad that I produced using FPRINTFSlash / after text .txtI need to read a file and store the values, so I can call these values in a for loop.
Best Answer