Is there a meaningful guideline on when to use parentheses vs squar... (2025)

"Use square brackets for concatination of strings....?"

If you want to concatenate string arrays (or any other type of array) then of course you can use square brackets.

Because square brackets are the concatenation operator.

Note that text comes in two major flavors, string arrays (here a scalar string array):

s = "hello world"

s = "hello world"

size(s)

ans = 1×2

1 1

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

and character arrays (here a character vector):

v = 'hello world'

v = 'hello world'

size(v)

ans = 1×2

1 11

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

They have very different properties and behaviors:

https://www.mathworks.com/help/matlab/characters-and-strings.html

"Defining a vector can use parentheses OR square brackets?"

This is a very common misunderstanding about MATLAB, which does really not have a vector defining operator in the sense that you mean (certainly parentheses (a grouping operator) will not help you with this), partly because it does not have a special vector type. Note that vectors are just arrays, and therefore may be defined by concatenating arrays (i.e. vectors or scalars aka numbers) together using square brackets.

Because square brackets are the concatenation operator.

"Indexing can use parentheses OR square brackets?"

If your courses used square brackets to perform indexing then you need to ask them for your money back.

MATLAB indexing fits generally into these two types:

  • () parentheses to refer to the array itself.
  • {} curly braces to refer to the array content (for container types only)

"To pass a value to a function use parentheses?"

Yes.

"Matrix concatenation use square brackets?"

All concatenation uses square brackets (see "defining a vector" above). Or CAT, HORZCAT, VERTCAT, etc.

Because square brackets are the concatenation operator.

"To construct matrix use square brackets?"

Yes, any matrix (i.e. 2D array) incuding vectors (which are just matrices after all) and scalars (although this is usually not useful) may be defined by concatenating other arrays (incuding scalar ones i.e. numbers) together. That is how MATLAB works. Everything is an array. Concatenation uses square brackets.

Because square brackets are the concatenation operator.

"Plot functions use parentheses"

Calling any function function uses parentheses, not just plotting:

cos(pi)

ans = -1

Looking over your list of specific questions, most of your confusion seems to relate to array concatenation. Perhaps this confusion is based on experience with other languages, thus you are seeking different special operators for different special data types e.g. scalars, vectors, matrices, etc. In MATLAB these differences simply do not exist. MATLAB has concatenation of arrays using square brackets. That is it.

What beginners often fail to understand is that in MATLAB everything is an array:

https://www.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-classes.html

Special names e.g. "scalar" or "matrix" or "vector" are purely for the humans reading the text, they do not indicate different data types or differences in memory or ways of creating them. Only the array size is different. Buts still arrays!

This is an array (a scalar one, aka a number):

a = 3

a = 3

Here I concatenate three (scalar) arrays together to make a larger array:

b = [1,2,9]

b = 1×3

1 2 9

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

Here I concatenate several arrays together to make a larger array (a matrix):

c = [0,0,a;b]

c = 2×3

0 0 3 1 2 9

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

Beginners often confuse themselves because they are incorrectly imagine that all of these things (creating vectors, creating matrices) are all different operations that require different magic incantations (because that is how language X does it). In MATLAB they are fundamentally all the same thing: concatenating arrays together using square brackets.

LIkewise, once you understand that numbers are just scalar arrays which are not a special class or type (they are just the same as any other array) then concatenating any arrays (e.g. scalars) together to make larger arrays (e.g. vectors, matrices) is a very simple concept to work with.

Do not search for differences (because that is how language X does it) that do not exist, otherwise you will just confuse yourself.

Is there a meaningful guideline on when to use parentheses vs squar... (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Terrell Hackett

Last Updated:

Views: 6282

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.