31

A bit of a newbie question: whats the difference between square brackets [] and curly brackets {} in Matlab? When is it appropriate to use either?

Update: its actually in the Matlab docs under "Special Characters".

1
  • As noted, your answer is probably in the documentation here. However, it's unclear what you mean by "angle" brackets, which typically refer to <>, but these symbols are not use to "bracket" anything in MATLAB as far as I know.
    – gnovice
    May 11, 2011 at 16:22

2 Answers 2

28

A square bracket creates a vector or matrix, whereas curly brackets creates a cell array.

When working with numbers, I'd say that 99% of the time, you will use square brackets. Cell arrays allow you to store different types of data at each location, e.g. a 10x5 matrix at (1,1), a string array at (1,2), ...

x = [1 2 3]; #% matrix with values 1, 2, 3
y = {1, 'a', x}; #% cell array storing a number, a character, and 1x3 matrix

Here is the MATLAB documentation on cell arrays: http://www.mathworks.com/help/matlab/cell-arrays.html

3
  • I think OP is wrong in his terminology. Angle brackets refer to <>, which are used as relational operators. [] are simply called brackets or square brackets.
    – abcd
    May 11, 2011 at 15:42
  • I see, yes, my answer is about [] vs {}.
    – AVH
    May 11, 2011 at 15:44
  • @yoda - you're absolutely correct. I've updated my question from "angel brackets" to "square brackets".
    – Contango
    May 16, 2011 at 10:46
2

This paper answered my question above in a very elegant manner. The paper explains Matlab arrays to someone who is more familiar with non-array based languages such as C++, C#, Java, and Python:

MATLAB array manipulation tips and tricks - Peter J. Acklam

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.