按照命令中显示的方式保存矩阵 window

Save a matrix the way it is displayed in the command window

我想将数据存储在一个文本文件中,其格式与它在命令 Window 中的显示方式完全相同。例如,

>> A = 20*randn(4,4)
A =
   -1.0984   25.0050  -13.0311  -38.9769
   18.2225   18.5958   23.8420   20.4100
   11.8917    4.7953  -32.2366   17.2343
    7.0040  -13.8072   -0.4892    0.0232

现在我想把这个矩阵存储在一个相同格式的txt文件中。如果我保存这个矩阵,输出看起来像(即编辑器是 notepad++ )

-1.0984,25.005,-13.031,-38.977
18.223,18.596,23.842,20.41
11.892,4.7953,-32.237,17.234
7.004,-13.807,-0.48924,0.023242

任何建议。

如果你想完全显示的那样保存输出,你可以使用diary:

% Save output to this file.
diary('file.txt')

A = 20 * randn(4, 4)

% Stop saving output.
diary

如果您只想保存矩阵,而不保存脚本的其他输出,那么您可以使用 dlmwrite 和制表符 \t 分隔符以及您选择的格式,例如:

dlmwrite('file.txt', A, 'delimiter', '\t', 'precision', '%.6f')