MATLAB: Fprintf can’t make new line

fprintf

I want to edit a line of a text file (tcl file actually). I tried with following code:
A = 5;
tcl = regexp(fileread('old_file.tcl'), '\n', 'split')';
tcl{3} = char(strcat("set A ",num2str(A),".0"));
fid = fopen('new_file.tcl','w');
fprintf(fid,'%s\n',tcl{:});
fclose(fid);
But it can't create new line after line 3. Instead, it create a character looks like small circle with black background, shown in figure below. I tried using fid==1 to check, it works fine in command window. How do I fix this? And if there is inefficiency in my code, suggestions are really appreciated. Thanks in advance.

Best Answer

Try opening the file in text mode, not binary mode:
A = 5;
tcl = regexp(fileread('old_file.tcl'), '[\n\r]+', 'split')';
tcl{3} = sprintf('set A %u.0',A);
fid = fopen('new_file.tcl','wt'); % text mode!
fprintf(fid,'%s\n',tcl{:});
fclose(fid);