Matlab将单元格数组保存到文本文件 [英] Matlab saving cell array to text file

查看:689
本文介绍了Matlab将单元格数组保存到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索mathworks的网站之后,我也在这里找到了应该用于将单元格数据保存到文本文件中的代码...但是我发现的每个变体都不起作用.这是我当前的代码(以及在这里和MathWorks上出现最多的代码)-请帮助我弄清楚为什么它对我不起作用...:

After searching mathworks' website and here too I managed to find the code that is SUPPOSED to work for saving cell data to a text file... but every variation I found doesn't work. Here's my current code (and the one that has appeared the most here and on mathworks) - please help me figure out why it's not working for me...:

第一次尝试:

array = cell(1,10);
for i=1:10
    array{i} = 'someText';
end
fid = fopen('file.txt', 'wt');
fprintf(fid, '%s\n', array);
fclose(fid);

错误:

使用fprintf时出错 没有为单元格"输入定义功能.

Error using fprintf Function is not defined for 'cell' inputs.

saveToFile错误(第11行) fprintf(fid,'%s \ n',array);

Error in saveToFile (line 11) fprintf(fid, '%s\n', array);

因此,我专门寻找了一种适用于细胞阵列的药物(可以在这里找到:

So I specifically looked for one that is good for cell-arrays (can be found here: http://www.mathworks.com/help/matlab/import_export/write-to-delimited-data-files.html)

第二次尝试:

array = cell(1,10);
for i=1:10
    array{i} = 'someText';
end
fileID = fopen('celldata.dat','w');
[nrows,ncols] = size(array);
for row = 1:nrows
fprintf(fileID,'%s\n' ,array{row,:});
end
fclose(fileID);

错误:

使用fprintf时出错 没有为单元格"输入定义功能.

Error using fprintf Function is not defined for 'cell' inputs.

saveToFile错误(第12行) fprintf(fileID,'%s \ n',array {row,:});

Error in saveToFile (line 12) fprintf(fileID,'%s\n' ,array{row,:});

我将为您避免其他失败的尝试..这些是我能找到的最好的..任何帮助将不胜感激! :)

I will spare you some other failed attempts.. these were the best I could find.. Any help will be greatly appreciated! :)

推荐答案

或者,您可以使用strjoin将单元格数组连接成字符串:

Alternatively, you could use strjoin to join a cell array into a string:

array = cell(1,10);
for i=1:10
    array{i} = 'someText';
end
line = strjoin(array)
fid = fopen('file.txt', 'wt');
fprintf(fid, '%s\n', line);
fclose(fid);

这篇关于Matlab将单元格数组保存到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆