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.
Parentheses are for separating citations or other asides from the body text. Brackets show changes within quoted material. Braces —sometimes known as curly brackets—are not typically used except in technical and mathematical writing.
Curly brackets are rarely used in prose and have no widely accepted use in formal writing, but may be used to mark words or sentences that should be taken as a group, to avoid confusion when other types of brackets are already in use, or for a special purpose specific to the publication (such as in a dictionary).
A square bracket creates a vector or matrix, whereas curly brackets creates a cell array.
Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.
Think of cell array as a regular homogenic array, whose elements are all cell
s. Parentheses (()
) simply access the cell
wrapper object, while accessing elements using curly bracers ({}
) gives the actual object contained within the cell.
For example,
A={ [5,6], 0 , 0 ,0 };
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 do foo(i) = []
you remove the i-th cell. It is not an assignment operation, but rather a RemoveElement
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With