这与帖子有关
display all elements in a nested cell array (with character entries)
更改条目是字符。要求澄清一个新问题。
现在:
a =
{1x10 cell} {1x10 cell} {1x10 cell} {1x10 cell}
一个{:} =
ans = [0] [0.4000] [0] [0] [0] [0] [0] [0] [0] [0]
ans = [0] [0] [0.2000] [0] [0.2000] [0] [0.2000] [0] [0] [0]
ans = [0] [0] [0] [0] [0] [0.2000] [0] [0] [0.2000] [0.2000]
ans = [0] [0.2000] [0] [0] [0] [0] [0] [0.4000] [0] [0.2000]
之前的答案是:
fileID = fopen('a.txt', 'at');
fprintf(fileID, '%2.8s \n', cellfun(@(x) char(x), a));
fclose(fileID);
现在如何解决? 想打印:
0 0.4 0 0 0 0 0 0 0 0
0 0 0.2 0 0.2 0 0.2 0 0 0
.
.
感谢
答案 0 :(得分:2)
我似乎记得如果将值放入数组中,它将适当地进行转换。我没有Matlab来测试它,但这应该可行。
[a{:}]
答案 1 :(得分:1)
这是一种方法:
a = { { 0, 0.4000, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0.2000, 0, 0.2000, 0, 0.2000, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0.2000, 0, 0, 0.2000, 0.2000 }, { 0, 0.2000, 0, 0, 0, 0, 0, 0.4000, 0, 0.2000 }};
fileID = fopen('a.txt', 'at');
fprintf(fileID, [ (regexprep((regexprep((regexprep((regexprep(mat2str(cell2mat(cellfun(@cell2mat, a, 'UniformOutput', false)')), '(0 )' , '$1 ')), '[', '')), ']', '')), ';', '\n')), '\n' ]);
fclose(fileID);
编辑:替代解决方案。在这一行中,较短的行用空格填充。
CharMatrix = char(regexprep(cellfun(@mat2str, (cellfun(@cell2mat, a, 'UniformOutput', false)'), 'UniformOutput', false), '0 ', '0 '));
CharMatrix(CharMatrix == ']') = ' ';
CharMatrix(:,1) = [];
CharMatrix(:,end) = '\';
CharMatrix(:,end+1) = 'n';
fileID = fopen('a.txt', 'at');
fprintf(fileID, reshape(CharMatrix', 1, []));
fclose(fileID);