【问题标题】:How can I write strings and matrices to a .txt file in MATLAB?如何在 MATLAB 中将字符串和矩阵写入 .txt 文件?
【发布时间】:2010-12-29 19:01:27
【问题描述】:

我需要将数据写入 MATLAB 中的 .txt 文件。我知道如何编写字符串 (fprintf) 矩阵 (dlmwrite),但我需要一些可以同时做到这两个的东西。下面我举个例子:

str = 'This is the matrix: ' ;
mat1 = [23 46 ; 56 67] ;
%fName
if *fid is valid* 
    fprintf(fid, '%s\n', str)
    fclose(fid)
end
dlmwrite(fName, *emptymatrix*, '-append', 'delimiter', '\t', 'newline','pc')
dlmwrite(fName, mat1, '-append', 'newline', 'pc')

这工作正常,但有一个问题。文件的第一行是:

This is the matrix: 23,46

这不是我想要的。我想看:

This is the matrix:
23 46
56 67

我该如何解决这个问题?我不能使用 for 循环和 printf 解决方案,因为数据量很大而且时间是个问题。

【问题讨论】:

    标签: string matlab matrix file-io text-files


    【解决方案1】:

    我认为解决问题所需要做的就是在 FPRINTF 语句中添加回车符 (\r) 并删除对 DLMWRITE 的第一次调用:

    str = 'This is the matrix: ';      %# A string
    mat1 = [23 46; 56 67];             %# A 2-by-2 matrix
    fName = 'str_and_mat.txt';         %# A file name
    fid = fopen(fName,'w');            %# Open the file
    if fid ~= -1
      fprintf(fid,'%s\r\n',str);       %# Print the string
      fclose(fid);                     %# Close the file
    end
    dlmwrite(fName,mat1,'-append',...  %# Print the matrix
             'delimiter','\t',...
             'newline','pc');
    

    文件中的输出如下所示(数字之间有制表符):

    This is the matrix: 
    23  46
    56  67
    


    注意: 一个简短的解释......在FPRINTF 语句中需要\r 的原因是因为PC 行终止符由回车符后跟一个换行符,这是 DLMWRITE 在指定 'newline','pc' 选项时使用的内容。在记事本中打开输出文本文件时,需要\r 以确保矩阵的第一行出现在新行上。

    【讨论】:

    • 感谢新手!你又帮了我一次!
    • 我有一个相关的问题:代码在我的系统上正常工作。但是,当我将桌面远程连接到服务器并将其 Matlab 路径设置为我的本地 matlab 目录时,相同的代码无法设置文件。整个项目运行正常,但文件过程失败。有cmets吗?谢谢
    • pathstr = 'C:/Documents and Settings/xxx/My Documents/MATLAB/Factor Production/Results/xxx' ;当我在本地执行此路径时,它工作正常。但是,当我使用服务器并运行同一组文件(通过设置路径)时,这会失败。我收到错误“没有这样的文件或目录”。消息来自代码行: [fid,message] = fopen(fName,'w') ;
    • 谢谢。我只是将结果输出到另一台服务器。它很好地达到了目的!感谢您的帮助。
    【解决方案2】:

    您不需要空矩阵调用。试试这个代码:

    str = 'This is the matrix: ' ;
    mat1 = [23 46 ; 56 67] ;
    fName = 'output.txt';
    fid = fopen('output.txt','w');
    if fid>=0
        fprintf(fid, '%s\n', str)
        fclose(fid)
    end
    dlmwrite(fName, mat1, '-append', 'newline', 'pc', 'delimiter','\t');
    

    【讨论】:

      【解决方案3】:

      您有两个 dlmwrite() 调用,第一个在空矩阵上,第二个缺少“分隔符”选项。如果将其添加到第二个调用中会发生什么?

      【讨论】:

        【解决方案4】:

        我遇到了类似的情况,向 csv 添加标题。您可以使用 dlmwrite 和 -append 通过将分隔符设置为等于 '' 来添加一行,如下所示。

        str = 'This is the matrix: ';      %# A string
        mat1 = [23 46; 56 67];             %# A 2-by-2 matrix
        fName = 'str_and_mat.txt';         %# A file name
        header1 = 'A, B'
        dlmwrite(fName, str, 'delimiter', '')
        dlmwrite(fName, header1, '-append', 'delimiter', '')
        dlmwrite(fName, mat1, '-append','delimiter', ',')
        

        这会产生以下内容:

        This is the matrix: 
        A, B
        23,46
        56,67
        

        【讨论】:

          猜你喜欢
          • 2015-01-30
          • 1970-01-01
          • 2011-02-27
          • 2013-01-13
          • 1970-01-01
          • 2012-01-27
          • 2015-02-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多