[英]Dimensions of matrices being concatenated are not consistent using array with characters
I'm trying to initialize 我正在尝试初始化
labels =['dh';'Dh';'gj';'Gj';'ll';'Ll';'nj';'Nj';'rr';'Rr';'sh';'Sh';'th';'Th';'xh';'Xh';'zh';'Zh';'ç';'Ç';'ë';'Ë'];
But it shows me the error on title.When I try with numbers it's all perfect but not with characters.What could be the problem? 但是它告诉我标题上的错误。当我尝试使用数字时,这是完美的,但对于字符来说不是。这可能是什么问题?
If you wish to eliminate any padding, you can also store it into a cell as follows. 如果您希望消除任何填充,也可以按以下方式将其存储到单元中。
labels = {'dh';'Dh';'gj';'Gj';
'll';'Ll';'nj';'Nj';
'rr';'Rr';'sh';'Sh';
'th';'Th';'xh';'Xh';
'zh';'Zh';'ç';'Ç';
'ë';'Ë'};
Then you can reference the "i"th element using labels{i}
instead of labels(i,:)
which is simpler. 然后,您可以使用labels{i}
而不是更简单的labels(i,:)
来引用第“ i”个元素。 You can further run more string operations using cellfun
and not interfere with any existing values that you've stored. 您可以使用cellfun
进一步运行更多的字符串操作,并且不会干扰已存储的任何现有值。
I agree with krisdestruction that using a cell array makes the code accessing the strings simpler and is generally more idiomatic. 我同意krisdestruction的观点 ,即使用单元格数组可使访问字符串的代码更简单,并且通常更惯用。 That is what I would also recommend unless there is a compelling reason to do something else. 除非有迫不得已的理由要做其他事情,否则我也建议这样做。
For completeness, you could use the char
function to add the padding automatically for you if you really want a character array: 为了完整起见,如果您确实需要字符数组,可以使用char
函数为您自动添加填充:
>> char('aa','bb','c')
ans =
aa
bb
c
where the last row is 'c '
. 最后一行是'c '
。 From the char
documentation: 从char
文档中:
S = char(A1,...,AN) converts the arrays A1,...,AN into a single character array. S = char(A1,...,AN)将数组A1,...,AN转换为单个字符数组。 After conversion to characters, the input arrays become rows in S. Each row is automatically padded with blanks as needed . 转换为字符后,输入数组成为S中的行 。 根据需要,每行将自动用空白填充 。 An empty string becomes a row of blanks. 空字符串变成一行空白。
(Emphasis mine) (强调我的)
From the Mathworks documentation : 从Mathworks文档中 :
Apply the MATLAB concatenation operator, []. 应用MATLAB串联运算符[]。 Separate each row with a semicolon (;). 用分号(;)分隔每行。 Each row must contain the same number of characters . 每行必须包含相同数量的字符 。 For example, combine three strings of equal length: 例如,组合三个等长的字符串:
You can try padding like this to make every row 2 characters: 您可以尝试这样填充以使每行2个字符:
labels = ['dh';'Dh';'gj';'Gj';
'll';'Ll';'nj';'Nj';
'rr';'Rr';'sh';'Sh';
'th';'Th';'xh';'Xh';
'zh';'Zh';'ç ';'Ç ';
'ë ';'Ë '];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.