Writing a new line to a 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 new line, the generated file:

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

What is the problem with my code?

+5
source share
2 answers

Use sprintfto make these lines:

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

As you do this now, it refers to the backslash as a literal.

+5
source

Convert '\ n' to double before inserting it into a string:

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

Thanks to Franck Dernoncourt, a research fellow at Adobe Research.

0
source

All Articles