Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing new line into text file in matlab

function [org_data] = file_manipulation(in_fname, txt_fname, mat_fname)
    org_data = round(load(in_fname));

    fid = fopen(txt_fname,'wt+');
    student_id = '9900';
    txt = [txt_fname ' : ' student_id '\nDate of creation:' datestr(now,'dd/mm/yyyy')]; 
    fprintf(fid,'%s',txt);

end

Instead of inserting a newline the file generated is:

C:\w2\test1.txt : 9900\nDate of creation:30/05/2012

What's the problen with my code?

like image 946
Day_Dreamer Avatar asked May 30 '12 17:05

Day_Dreamer


2 Answers

Use sprintf to make those strings:

fprintf(fid, sprintf('%s : %s\nDate of creation: %s', txt_fname, student_id, datestr(now,'dd/mm/yyyy')));

The way you're doing it now, it treats the backslash as a literal.

like image 105
Ansari Avatar answered Oct 19 '22 22:10

Ansari


Convert the '\n' to double before you insert it to the string:

fid = fopen('my_file.txt', 'w');
fwrite(fid, ['First line' double(sprintf('\n')) 'Second line'])
fclose(fid);

Thanks to Franck Dernoncourt, Reseach Scientist at Adobe Research.

like image 26
Moti Hamo Avatar answered Oct 19 '22 21:10

Moti Hamo