如何将元胞数组附加到 .txt 文件?

发布于 2024-10-09 22:33:21 字数 945 浏览 0 评论 0原文

我之前查询过在 .txt 中包含矩阵和字符串文件。我现在需要将单元格附加到它。从我之前的问题来看:

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

现在我想附加一个字符串:'删除的标识符是',然后在它下面的元胞数组:

'ABC' [10011] [2]
'DEF' [10023] [1] 

一些相关链接:

http://www.mathworks.com/help/techdoc/ref/fileformats.htmlhttp://www.mathworks.com/support/解决方案/en/data/1-1CCMDO/index.html?solution=1-1CCMDO

I previously queried about including matrices and strings in a .txt file. I now need to append cells to it. From my prior question:

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

Now I want to append a string: 'The removed identifiers are' and then this cell array below it:

'ABC' [10011] [2]
'DEF' [10023] [1] 

Some relevant links:

http://www.mathworks.com/help/techdoc/ref/fileformats.html, http://www.mathworks.com/support/solutions/en/data/1-1CCMDO/index.html?solution=1-1CCMDO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇妻 2024-10-16 22:33:21

不幸的是,您无法使用 DLMWRITECSVWRITE 用于写入数据元胞数组。但是,要获得所需的输出,您仍然可以使用一次调用 FPRINTF< /a>,但您必须指定元胞数组一行中所有条目的格式。以我对您上一个问题的回答为基础,您将添加这些附加行:

str = 'The removed identifiers are: ';   %# Your new string
cMat = {'ABC' 10011 2; 'DEF' 10023 1};   %# Your cell array
fid = fopen(fName,'a');                  %# Open the file for appending
fprintf(fid,'%s\r\n',str);               %# Print the string
cMat = cMat.';                          %'# Transpose cMat
fprintf(fid,'%s\t%d\t%d\r\n',cMat{:});   %# Print the cell data
fclose(fid);                             %# Close the file

新文件内容(包括旧示例)将如下所示:

This is the matrix: 
23  46
56  67
The removed identifiers are: 
ABC 10011   2
DEF 10023   1

Unfortunately, you can't use functions like DLMWRITE or CSVWRITE for writing cell arrays of data. However, to get the output you want you can still use a single call to FPRINTF, but you will have to specify the format of all the entries in a row of your cell array. Building on my answer to your previous question, you would add these additional lines:

str = 'The removed identifiers are: ';   %# Your new string
cMat = {'ABC' 10011 2; 'DEF' 10023 1};   %# Your cell array
fid = fopen(fName,'a');                  %# Open the file for appending
fprintf(fid,'%s\r\n',str);               %# Print the string
cMat = cMat.';                          %'# Transpose cMat
fprintf(fid,'%s\t%d\t%d\r\n',cMat{:});   %# Print the cell data
fclose(fid);                             %# Close the file

And the new file contents (including the old example) will look like this:

This is the matrix: 
23  46
56  67
The removed identifiers are: 
ABC 10011   2
DEF 10023   1
嘿看小鸭子会跑 2024-10-16 22:33:21

您可以使用 File Exchange 中的 cellwrite。阅读 Francis Barnhart使用 MATLAB 写入混合数据 ,cellwrite 的创建者可能值得一看。

更改 cellwrite 的签名以接受文件句柄应该是一项可行的任务。允许将数据附加到已存在的文件中。

You may use cellwrite from File Exchange. Reading Writing Mixed Data With MATLAB from Francis Barnhart, the creator of cellwrite might be worth a look.

It should be a feasible task, to change cellwrite's signature to accept a file handle. Allowing to append data to an already existing file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文