如果您只是想将变量名保存为可读格式,您应该查看dlmwrite 函数。它会在您的情况下使用分隔符、空格或制表符保存您的变量。
bigMatrix = rand(10);
dlmwrite('myFile.txt',bigMatrix,'delimiter',' ');
如果你想使用制表符分隔,你可以使用以下行
dlmwrite('myFile.txt',bigMatrix,'delimiter','\t')
如果您想进一步指定精度,可以使用以下代码指定precision = 3 sig figs。
dlmwrite('myFile.txt',bigMatrix,'delimiter','\t','precision',3)
此外,您可以使用字符串指定恒定精度以获得恒定的十进制精度。指定4f 将提供您在format short 中看到的内容,而16f 将提供您在format long 中看到的内容。
dlmwrite('myFile.txt',bigMatrix,'delimiter','\t','precision','%.4f');
dlmwrite('myFile.txt',bigMatrix,'delimiter','\t','precision','%.16f');
控制台结果
0.0157 0.8161 0.1660 0.8044 0.7012 0.8915 0.6966 0.2325 0.9770 0.6212
0.5967 0.1195 0.3072 0.6632 0.5709 0.3407 0.5644 0.4358 0.5343 0.1799
0.3570 0.2627 0.4324 0.2399 0.8350 0.4752 0.1126 0.5609 0.8926 0.5254
0.8675 0.6808 0.5898 0.7436 0.3731 0.5500 0.9843 0.7279 0.9430 0.0010
0.0743 0.4527 0.7377 0.2043 0.2759 0.0786 0.9285 0.3956 0.4404 0.3718
0.3304 0.4124 0.4244 0.0284 0.5514 0.1419 0.5662 0.3812 0.8652 0.9084
0.0137 0.2696 0.1927 0.3641 0.2758 0.9919 0.1418 0.4953 0.2369 0.4871
0.6338 0.6510 0.0933 0.4455 0.6999 0.0452 0.9040 0.0693 0.5381 0.2402
0.3475 0.2850 0.0704 0.1310 0.4020 0.8573 0.9005 0.0485 0.1248 0.4577
0.6318 0.9094 0.8535 0.8341 0.3268 0.7237 0.3242 0.6747 0.6252 0.0885
文件结果
0.0157 0.8161 0.1660 0.8044 0.7012 0.8915 0.6966 0.2325 0.9770 0.6212
0.5967 0.1195 0.3072 0.6632 0.5709 0.3407 0.5644 0.4358 0.5343 0.1799
0.3570 0.2627 0.4324 0.2399 0.8350 0.4752 0.1126 0.5609 0.8926 0.5254
0.8675 0.6808 0.5898 0.7436 0.3731 0.5500 0.9843 0.7279 0.9430 0.0010
0.0743 0.4527 0.7377 0.2043 0.2759 0.0786 0.9285 0.3956 0.4404 0.3718
0.3304 0.4124 0.4244 0.0284 0.5514 0.1419 0.5662 0.3812 0.8652 0.9084
0.0137 0.2696 0.1927 0.3641 0.2758 0.9919 0.1418 0.4953 0.2369 0.4871
0.6338 0.6510 0.0933 0.4455 0.6999 0.0452 0.9040 0.0693 0.5381 0.2402
0.3475 0.2850 0.0704 0.1310 0.4020 0.8573 0.9005 0.0485 0.1248 0.4577
0.6318 0.9094 0.8535 0.8341 0.3268 0.7237 0.3242 0.6747 0.6252 0.0885