Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:48   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何将单元格数组附加到.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/techdo...leformats.htmlhttp://www.mathworks.com/support/solutions/en/data/1-1CCMDO/index.html?solution=1- 1个CDO



回答:

不幸的是,您不能使用DLMWRITECSVWRITE之类的函数来写入数据单元格数组。但是,要获得所需的输出,您仍然可以使用FPRINTF的单个调用,但是您必须指定单元格阵列的一行中所有条目的格式。在我对上一个问题的回答的基础上 ,您将添加以下附加行:

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

更多&回答...
poster 当前离线   回复时引用此帖
 

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 04:53


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.