matlab - 将带有数字的单元格保存到文本文件
问题描述
我有不同长度和数字的单元格数组 A 和 B。
A={1:0.5:5;1:0.5:2};
B={1:0.5:6;1:0.5:9};
C= [A;B];
我想将这些元胞数组组合成一个元胞数组 C,它看起来像这样:
C =
4×1 cell array
{1×9 double}
{1×3 double}
{1×11 double}
{1×17 double}
然后,我想把它保存到一个文本文件中,应该是这样的:
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000
1.0000 1.5000 2.0000
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000
到目前为止,我只找到了文本或相同大小数组的代码。这是我的尝试,它不起作用:
fid = open('filename.txt', 'wt');
fprintf(fid, '%f',C{:})
close(fid)
解决方案
我相信问题可能出在您为 指定的格式中fprintf
,因为我相信使用 only'%f'
会在每一行上打印一个数字。
这样做的一种方法是:
fid = fopen('filename.txt', 'wt');
for i = 1:length(C)
fmt = repmat('%f ',size(C{i})); % this only adds one whitespace in between numbers
fmt = [fmt,'\n']; % remember to add a new line
fprintf(fid,fmt,C{i});
end
fclose(fid);
推荐阅读
- ios - Objective-C 项目中的 Swift CocoaPods 库
- javascript - 有没有办法缩短 API 端点 URL,然后针对它发出请求?
- javascript - 如何在firebas云功能中使用localStorage
- sql - 存储过程中 select 和 sp_executesql 之间的安全权限差异
- storybook - Storybook.js - DSM 集成:加载问题
- java - 如何在java中使用正则表达式识别单词(不区分大小写)
- java - 在 java servlet 中运行覆盖的 doPost 方法时遇到问题
- javascript - Vuex 操作不等待完成 axios 承诺
- python - Numpy 矩阵行列式没有按预期工作?
- r - knitr 和简单执行模式中的 set.seed()