【问题标题】:Export Matlab Matrix as .txt without commas in between values将 Matlab 矩阵导出为 .txt,值之间没有逗号
【发布时间】:2014-03-28 00:05:54
【问题描述】:

我需要以 .txt 格式从 Matlab 中导出一个矩阵,并且我不想在值之间使用任何逗号。我还需要新行中的每一行。例如:

A = [ 1 2 3 4;5 6 7 8 ]

我需要的.txt格式:

1 2 3 4

5 6 7 8

谢谢。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    使用num2str:

    A = [ 1 2 3 4;5 6 7 8 ]
    str = num2str(A);
    

    给予

    str =
    1  2  3  4
    5  6  7  8
    

    然后使用fprintf 将该字符串打印到文件中,或者使用diary

    diary('filename.txt')
    disp(str)
    diary off 
    

    你也可以使用

    save('filename.txt','A','-ascii')
    

    【讨论】:

      【解决方案2】:

      直接fprintf矩阵到文件:

      fid = fopen('A.txt','w')
      fprintf(fid,[repmat('%g ',1,size(A,2)) '\n'],A)
      fclose(fid)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多