Main Content

period, .

Decimal point, element-wise operations, indexing

Syntax

Description

The period symbol (.) separates the integral and fractional parts of a number, denotes element-wise operations, and also indexes table variables, structure fields, and object properties and methods. For instance, the element-wise operation A.*B multiplies each element of A with the corresponding element in B.

example

Examples

expand all

Create a matrix and then use .^ to square each element. The operations A.^2 and A^2 are different when A is not a scalar.

A = [2 1 3
     3 2 1
     1 3 2];
A.^2
ans = 3×3

     4     1     9
     9     4     1
     1     9     4

Use dot indexing to access data in tables, structures, and objects.

Create a table with two variables, and then index into the second variable using the default variable name Var2.

T = table((1:4)',5*ones(4,1));
T.Var2
ans = 4×1

     5
     5
     5
     5

Create a structure with one field, and then query the field value.

S = struct("field",1);
S.field
ans = 
1

You also can use dot indexing with objects to set property values, query property values, or invoke methods.

myObj.property = 1;
val = myObj.property
myObj.method(arg1,arg2,..)

Tips

  • You can index structure fields with expressions using commands of the form S.(expr). The variable or expression in parentheses must evaluate to a string scalar. For more information, see Generate Field Names from Variables.

Version History

Introduced before R2006a