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

标签 matlab file-io text-files cell-array

我之前查询过including matrices and strings in a .txt file 。我现在需要将单元格附加到它。根据我之前的问题:

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/solutions/en/data/1-1CCMDO/index.html?solution=1-1CCMDO

最佳答案

不幸的是,您不能使用像 DLMWRITE 这样的函数或CSVWRITE用于写入数据元胞数组。但是,要获得所需的输出,您仍然可以使用对 FPRINTF 的单个调用,但您必须指定元胞数组的一行中所有条目的格式。建立在 my answer to your previous question 之上,您将添加这些附加行:

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

关于matlab - 如何将元胞数组附加到 .txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4564955/

相关文章:

带有进度条的 Apache IO 下载文件的 Java

java - java中如何知道jar文件的目录?

arrays - Matlab:如果索引在一个范围内,则对相应的值求和

Matlab——带边界的随机游走,矢量化

C++非空终止字符数组输出

java - 使用方法输出到用户决定的文本文件

excel - 如何从 Excel 或记事本(以 .txt 结尾的文档)将数据加载到 Win Bugs 中?

matlab - 如何计算 Matlab 中 2 个数组(一个是另一个数组的一部分)中值的出现次数?

c - 关于命名管道的简短阅读(matlab->linux)

java - 如何使 JFileChooser 打开显示当前工作目录,无论使用什么操作系统?