I am trying to make a text file with the contents of a cell array. Below is a MWE. When I use fprintf
without the file argument (to just print in the command window, e.g., fprintf(['\t\\hline ',repmat('%s & ',1,size(A,1)),'\b\b','\\\\','\n'],A{:})
), the code works just fine. However, when I open a file with fopen
and try to write the same thing to it, the double backspace (\b\b
) does not work.
MWE:
A = num2cell(rand(3,3));
fid = fopen('test.txt','w');
fprintf(fid,['\t\\hline ',repmat('%s & ',1,size(A,1)),'\b\b','\\\\','\n'],A{:})
What I get in the command window:
\hline 7.922073e-01 & 9.594924e-01 & 6.557407e-01 \\
\hline 3.571168e-02 & 8.491293e-01 & 9.339932e-01 \\
\hline 6.787352e-01 & 7.577401e-01 & 7.431325e-01 \\
What I get in the 'test.txt' file:
\hline 7.922073e-01 & 9.594924e-01 & 6.557407e-01 & \\
\hline 3.571168e-02 & 8.491293e-01 & 9.339932e-01 & \\
\hline 6.787352e-01 & 7.577401e-01 & 7.431325e-01 & \\
And when pasting the contents of 'test.txt' to this question, rather than showing up exactly as they look in the .txt file, some boxes showed up where the backspaces should have been applied (figure below). How can I get the backspaces to work and delete &
in the .txt file?