MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何将单元格数组附加到.txt文件? (https://www.labfans.com/bbs/showthread.php?t=24281)

poster 2019-12-10 20:48

如何将单元格数组附加到.txt文件?
 
我之前曾询问过要[URL="https://stackoverflow.com/questions/4556971/writing-to-a-txt-file-in-matlab-strings-matrices-cells"]在.txt文件中包含矩阵和字符串[/URL] 。我现在需要在其上附加单元格。从我之前的问题:

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'); 现在,我想附加一个字符串:' [I]删除的标识符为[/I] ',然后是其下面的此单元格数组:

'ABC' [10011] [2] 'DEF' [10023] [1] 一些相关链接:

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



[B]回答:[/B]

不幸的是,您不能使用[URL="http://www.mathworks.com/help/techdoc/ref/dlmwrite.html"]DLMWRITE[/URL]或[URL="http://www.mathworks.com/help/techdoc/ref/csvwrite.html"]CSVWRITE之[/URL]类的函数来写入数据单元格数组。但是,要获得所需的输出,您仍然可以使用[URL="http://www.mathworks.com/help/techdoc/ref/fprintf.html"]FPRINTF[/URL]的单个调用,但是您必须指定单元格阵列的一行中所有条目的格式。在[URL="https://stackoverflow.com/questions/4556971/writing-to-a-txt-file-in-matlab-strings-matrices-cells/4557188#4557188"]我对上一个问题的回答的基础上[/URL] ,您将添加以下附加行:

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

[url=https://stackoverflow.com/questions/4564955]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:03

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