inserting +- (i.e. \pm) between two numbers in matlab

前端 未结 3 663
南旧
南旧 2021-01-15 04:56

I want to insert the symbol +- (\\pm) between x and y in table created in matlab

x = (1:1:5)\';
y = x*5/100;
table = [x y]

So, that the out

3条回答
  •  醉酒成梦
    2021-01-15 06:00

    You can use unicode characters in MATLAB. The following works:

    >> fprintf('%f ± %f\n', table.')
    1.000000 ± 0.050000
    2.000000 ± 0.100000
    3.000000 ± 0.150000
    4.000000 ± 0.200000
    5.000000 ± 0.250000
    

    Note that fprintf cycles through all the elements of the input matrix in storage order (down the first column first). So it was necessary to transpose the data array (table.') to print it in one command.

    This works for printing to file as well on MacOS:

    f = fopen('mytextfile.txt','wt');
    fprintf(f,'%f ± %f\n', table.');
    fclose(f);
    

提交回复
热议问题