I need to write data to a .txt file in MATLAB. I know how to write strings (fprintf) or matrices (dlmwrite), but I need something that can do
fprintf
dlmwrite
You don't need the empty matrix call. Try this code:
str = 'This is the matrix: ' ; mat1 = [23 46 ; 56 67] ; fName = 'output.txt'; fid = fopen('output.txt','w'); if fid>=0 fprintf(fid, '%s\n', str) fclose(fid) end dlmwrite(fName, mat1, '-append', 'newline', 'pc', 'delimiter','\t');