Search code examples
matlabmatrixprecisionsignificant-digits

writing matrix columns with different precisions(significant digits) in matlab


I have a matrix m=[10 15.675; 13.5 34.987; 20 55.5]; I want to write this matrin on o txt file. I want to use different precisions for each columns. For example 1 for 1st column and 2 for 2nd column.

Desired output:

10.0 15.68

13.5 34.99

20.0 55.50

I am currently using dlmwrite(fileName, m,'-append','delimiter','\t','precision',2,'roffset' ,1,'newline','pc');

However, this applies 2 significant after '.' for all columns.

Is there any way to apply different significant digits for each column of a matrix?


Solution

  • maybe something simple like this?

    m = [10 15.675; 13.5 34.987; 20 55.5];
    file = fopen('file.txt', 'w');
    
    for ii = 1:size(m, 1)
        fprintf(file, '%0.1f %0.2f\n', m(ii, 1), m(ii, 2));
    end
    

    I've edited to add the '\n'