如何将元胞数组附加到 .txt 文件?
我之前查询过在 .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.html,http://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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,您无法使用 DLMWRITE 或 CSVWRITE 用于写入数据元胞数组。但是,要获得所需的输出,您仍然可以使用一次调用 FPRINTF< /a>,但您必须指定元胞数组一行中所有条目的格式。以我对您上一个问题的回答为基础,您将添加这些附加行:
新文件内容(包括旧示例)将如下所示:
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:
And the new file contents (including the old example) will look like this:
您可以使用 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.