Save text files without an empty line in the end?

19 views (last 30 days)
For some reason I need to save a matrix to a .txt file without an extra blank line in the end. For example, when I save [1;2;3] to a text file, the file will have an empty forth row, which I want to get rid of. I've tried 'save', 'dlmwrite', 'fprintf',...but none of them worked.

Accepted Answer

Thorsten
Thorsten on 21 Jan 2016
Edited: Thorsten on 21 Jan 2016
Use fprintf with newline \n for all but the last entry in x, which is written separately without \n:
x = [1;2;3];
fid = fopen('foo.txt', 'w');
fprintf(fid, '%f\n', x(1:end-1))
fprintf(fid, '%f', x(end))
fclose(fid)

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!