显示嵌套单元格数组中的所有元素(带有字符条目) [英] display all elements in a nested cell array (with character entries)
问题描述
我有以下内容:
a =
{1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}
其中:
a{:}
ans =
'a'
ans =
'a'
ans =
'c'
ans =
'a'
我要输入以下字符:a a c a
因为我需要使用fprintf
fprintf
不接受a{:}
如果我这样做,a{1}{:}
将仅考虑第一个字符a
If I do a{1}{:}
will consider only the first character a
如何解决此问题?谢谢.
How to fix this? Thanks.
推荐答案
如果只需要字符向量'aaca',则可以使用以下方法:
If you only need the character vector 'aaca', you can use this:
a = {{'a'}, {'a'}, {'c'}, {'a'}};
a_CharVector = cellfun(@(x) char(x), a);
如果要使用字符向量'a a c a',则可以使用regexprep
添加空格:
If you want the character vector 'a a c a ', you can use regexprep
to add the spaces:
a_CharVectorWithSpaces = regexprep((cellfun(@(x) char(x), a)), '(.)', '$1 ');
要打印带有空格和换行符的字符,可以使用以下命令:
To print a a c a with spaces and newline you can use this:
fprintf([ regexprep((cellfun(@(x) char(x), a)), '(.)', '$1 '), '\n' ]);
删除了不必要的匿名功能.在这种情况下,不需要@(x)
.
unnecessary anonymous function removed. @(x)
is unnecessary in this case.
要获得字符向量"aaca",这是可行的:
To get character vector 'aaca' this works:
a_CharVector = cellfun(@char, a);
要获取字符向量'a a c a',可以使用以下代码:
And to get character vector 'a a c a ' you can use this:
a_CharVectorWithSpaces = regexprep((cellfun(@char, a)), '(.)', '$1 ');
要使用换行符打印文件,请执行以下操作:
To printf a a c a with newline:
fprintf([ regexprep((cellfun(@char, a)), '(.)', '$1 '), '\n' ]);
这篇关于显示嵌套单元格数组中的所有元素(带有字符条目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!