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

夙愿已清 提交于 2020-05-08 15:59:35

问题


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 output is

1.0000 +/- 0.0500
2.0000 +/- 0.1000
3.0000 +/- 0.1500
4.0000 +/- 0.2000
5.0000 +/- 0.2500

If we can also write the minus exactly below plus.


回答1:


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);



回答2:


With output as a text file use the format spec for fprintf such as

FileID=fopen('FileName.txt','w');

fprintf(FileID,['%1.4f ',177,' %1.4f\n'],[x';y'])



回答3:


fprintf(['%0.2f ' char(177) ' %0.2f\n'], [x;y]);


来源:https://stackoverflow.com/questions/22663254/inserting-i-e-pm-between-two-numbers-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!