使用大括号和圆括号访问单元格元素之间的区别
使用括号 ()
和大括号 {}
访问元胞数组中的元素有什么区别?
例如,我尝试使用 cell{4} = []
和 cell(4) = []
。在第一种情况下,它将第 4 个th 元素设置为 []
,但在第二种情况下,它擦除了单元格元素,即将单元格元素计数减少 1 。
What is the difference between accessing elements in a cell array using parentheses ()
and curly braces {}
?
For example, I tried to use cell{4} = []
and cell(4) = []
. In the first case it sets the 4th element to []
, but in the second case it wiped out the cell element, that is, reduced the cell element count by 1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将元胞数组视为常规同质数组,其元素都是
cell
。括号 (()
) 只需访问cell
包装器对象,而使用大括号 ({}
) 访问元素则给出包含在单元格中的实际对象细胞。例如,
将如下所示:
使元素等于
[]< 的语法/code> 带括号实际上是删除该元素的请求,因此当您要求执行
foo(i) = []
时,您会删除 i-th 细胞。它不是赋值操作,而是一个RemoveElement
操作,它使用与赋值类似的语法。但是,当您执行
foo{i} = []
时,您将为第 i 个单元格分配一个新值(这是一个空数组),从而清除该单元格的内容。Think of cell array as a regular homogenic array, whose elements are all
cell
s. Parentheses (()
) simply access thecell
wrapper object, while accessing elements using curly bracers ({}
) gives the actual object contained within the cell.For example,
Will look like this:
The syntax of making an element equal to
[]
with parentheses is actually a request to delete that element, so when you ask to dofoo(i) = []
you remove the i-th cell. It is not an assignment operation, but rather aRemoveElement
operation, which uses similar syntax to assignment.However, when you do
foo{i} = []
you are assigning to the i-th cell a new value (which is an empty array), thus clearing the contents of that cell.请参阅此链接中的帮助。正如您将看到的,使用括号 () 访问会给出单元格的子集(即子单元格),而花括号 {} 会给出您尝试访问的单元格的内容。
See the help in this link. As you'll see, accessing with parentheses (), gives you a subset of a cell (i.e. a sub-cell), while curly braces {} gives you the content of the cell you are trying to access.