Search code examples
matlabfilematrix

Can the writematrix command in matlab specify the number of decimal places?


The writematrix command can write matrices to text files. For example, for a matrix like the following

A = [1, 2, 3, 4];

It can be written to file a.txt using the writematrix command

A = [1, 2, 3, 4];
writematrix(A, 'a.txt', 'WriteMode', 'append', 'Delimiter', ' ');

The output is

1 2 3 4

But if I want the output like below

1.0000 2.0000 3.0000 4.0000

How do I go about using writematrix?


Solution

  • Convert A into a character array with num2str in the required format before using writematrix i.e.

    writematrix(num2str(A,'%.4f '), 'a.txt', 'WriteMode', 'append', 'Delimiter', 'tab');