【问题标题】:How to print an array to a .txt file in Matlab?如何在 Matlab 中将数组打印到 .txt 文件?
【发布时间】:2009-09-21 21:09:42
【问题描述】:

我刚开始学习Matlab,所以这个问题可能很基础:

我有一个变量

a=[2.3 3.422 -6.121 9 4.55]

我希望将值输出到这样的 .txt 文件:

2.3
3.422
-6.121
9
4.55

我该怎么做?

fid = fopen('c:\\coeffs.txt','w'); //this opens the file
//now how to print 'a' to the file??

【问题讨论】:

    标签: arrays matlab file-io text-files


    【解决方案1】:

    以下应该可以解决问题:

    fid = fopen('c:\\coeffs.txt','wt');  % Note the 'wt' for writing in text mode
    fprintf(fid,'%f\n',a);  % The format string is applied to each element of a
    fclose(fid);
    

    有关更多信息,请查看FOPENFPRINTF 的文档。

    【讨论】:

    • @gnovice 是的,确实如此。谢谢。你知道我可以在哪里查找一些基本的东西吗?这是因为我刚开始使用 Matlab,我经常发现自己被这些琐碎的东西困住了。
    • 我想我需要一个循环。不知何故,所有的值都是自己打印出来的!!
    • @eSKay:我为相关功能添加了几个链接。一般来说,MATLAB 有非常好的文档和示例/教程,包括版本和在线 MathWorks 网站 (mathworks.com)。在命令窗口中,HELP 命令 (mathworks.com/access/helpdesk/help/techdoc/ref/help.html) 通常非常有用。 ;)
    • @gnovice 在输入中得到不正确的结果,作为单独的问题发布,因为它需要格式化 stackoverflow.com/questions/1457177/…
    • 对于矩阵可以使用dlmwrite,例如如果A是矩阵那么你可以用这种方式保存它:dlmwrite('file.txt',A)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多