Sysquake Pro – Table of Contents

Sysquake for LaTeX – Table of Contents

# Variables

### Variable assignment

Assignment to a variable or to some elements of a matrix variable.

#### Syntax

var = expr (var1, var2, ...) = function(...)

#### Description

`var = expr` assigns the result of the expression
`expr` to the variable `var`. When the expression is
a naked function call, `(var1,var2,...) = function(...)`
assigns the value of the output arguments of the function to the different
variables. Usually, providing less variables than the function can provide
just discards the superfluous output arguments; however, the function can
also choose to perform in a different way (an example of such a function
is `size`, which returns the number of rows
and the number of columns of a matrix either as two numbers if there are
two output arguments, or as a 1-by-2 vector if there is a single output
argument). Providing more variables than what the function can provide
is an error.

Variables can store any kind of contents dynamically: the size and type can change from assignment to assignment.

A subpart of a matrix variable can be replaced with the
use of parenthesis. In this case,
the size of the variable is expanded when required; padding elements are
0 for numeric arrays and empty arrays `[]` for cell arrays and
lists.

#### See also

Operator (),
operator `{}`,
`clear`,
`exist`,
`for`,
`subsasgn`

### beginning

First index of an array.

#### Syntax

v(...beginning...) A(...beginning...) function e = C::beginning(obj, i, n)

#### Description

In an expression used as an index to access some elements
of an array, `beginning` gives the index of the first element (line
or column, depending of the context). It is always 1 for native arrays.

`beginning` can be overloaded for objects of used-defined classes.
Its definition should be have a header equivalent to
`function e=C::beginning(obj,i,n)`, where `C` is the name
of the class, `obj` is the object to be indexed, `i`
is the position of the index expression where `beginning` is used,
and `n` is the total number of index expressions.

#### See also

Operator (),
operator `{}`,
`beginning`,
`end`,
`matrixcol`,
`matrixrow`

### end

Last index of an array.

#### Syntax

v(...end...) A(...end...) function e = C::end(obj, i, n)

#### Description

In an expression used as an index to access some elements
of an array, `end` gives the index of the last element (line
or column, depending of the context).

`end` can be overloaded for objects of used-defined classes.
Its definition should be have a header equivalent to
`function e=C::end(obj,i,n)`, where `C` is the name
of the class, `obj` is the object to be indexed, `i`
is the position of the index expression where `end` is used,
`n` is the total number of index expressions.

#### Examples

Last 2 elements of a vector:

a = 1:5; a(end-1:end) 4 5

Assignment to the last element of a vector:

a(end) = 99 a = 1 2 3 4 99

Extension of a vector:

a(end + 1) = 100 a = 1 2 3 4 99 100

#### See also

Operator `()`,
operator `{}`,
`size`,
`length`,
`beginning`,
`matrixcol`,
`matrixrow`

### global persistent

Declaration of global or persistent variables.

#### Syntax

global x y ... persistent x y ...

#### Description

By default, all variables are *local* and created the first time they are
assigned to. Local variables can be accessed only from the body of the function where
they are defined, but not by any other function, even the ones they call. They are
deleted when the function exits. If the function is called recursively (i.e. if it
calls itself, directly or indirectly), distinct variables are defined for each call.
Similarly, local variables defined in the workspace using the command-line interface cannot be
referred to in functions.

On the other hand, *global variables* can be accessed by multiple functions
and continue to exist even after the function which created them exits.
Global variables must be declared with `global` in each function which
uses them. They can also be declared in the workspace. There exists only a single variable
for each different name.

Declaring a global variable has the following result:

- If a previous local variable with the same name exists, it is deleted.
- If the global variable does not exist, it is created and initialized with the
empty array
`[]`. - Every access which follows the declaration in the same function or workspace uses the global variable.

Like global variables, *persistent variables* are preserved between
function calls; but they cannot be shared between different functions. They are
declared with `persistent`. They cannot be declared outside a function.
Different persistent variables can have the same name in different functions.

#### Examples

Functions to reset and increment a counter:

function reset global counter; counter = 0; function value = increment global counter; counter = counter + 1; value = counter;

Here is how the counter can be used:

reset; i = increment i = 1 j = increment j = 2

#### See also

### matrixcol

First index in a subscript expression.

#### Syntax

A(...matrixcol...) function e = C::matrixcol(obj, i, n)

#### Description

In an expression used as a single subscript to access some elements
of an array `A(expr)`, `matrixcol` gives an array
of the same size as `A` where each element is the column index.
For instance for a 2-by-3 matrix, `matrixcol` gives the
2-by-3 matrix `[1,2,3;1,2,3]`.

In an expression used as the second of multiple subscripts to access some
elements of an array `A(...,expr)` or `A(...,expr,...)`,
`matrixcol` gives a row vector of length `size(A,2)` whose elements are
the indices of each column. It is equivalent to the range `(beginning:end)`.

`matrixcol` is useful in boolean expressions to select some
elements of an array.

`matrixcol` can be overloaded for objects of used-defined classes.
Its definition should have a header equivalent to
`function e=C::matrixcol(obj,i,n)`, where `C` is the name
of the class, `obj` is the object to be indexed, `i`
is the position of the index expression where `matrixcol` is used,
and `n` is the total number of index expressions.

#### Example

Set to 0 the NaN values which are not in the first column:

A = [1, nan, 5; nan, 7, 2; 3, 1, 2]; A(matrixcol > 1 & isnan(A)) = 0 A = 1 0 5 nan 7 2 3 1 2

#### See also

### matrixrow

First index in a subscript expression.

#### Syntax

A(...matrixrow...) function e = C::matrixrow(obj, i, n)

#### Description

In an expression used as a single subscript to access some elements
of an array `A(expr)`, `matrixrow` gives an array
of the same size as `A` where each element is the row index.
For instance for a 2-by-3 matrix, `matrixrow` gives the
2-by-3 matrix `[1,1,1;2,2,2]`.

In an expression used as the first of multiple subscripts to access some
elements of an array `A(expr,...)`,
`matrixrow` gives a row vector of length `size(A,1)` whose elements are
the indices of each row. It is equivalent to the range `(beginning:end)`.

`matrixrow` is useful in boolean expressions to select some
elements of an array.

`matrixrow` can be overloaded for objects of used-defined classes.
Its definition should be have a header equivalent to
`function e=C::matrixrow(obj,i,n)`, where `C` is the name
of the class, `obj` is the object to be indexed, `i`
is the position of the index expression where `matrixrow` is used,
and `n` is the total number of index expressions.

#### See also

### subsasgn

Assignment to a part of an array, list, or structure.

#### Syntax

A = subsasgn(A, s, B)

#### Description

When an assignment is made to a subscripted part of an object in
a statement like `A(s1,s2,...)=B`, LME executes `A=subsasgn(A,s,B)`,
where `subsasgn` is a method of the class of variable `A`
and `s` is a structure with two fields: `s.type` which
is `'()'`, and `s.subs` which is the list of subscripts
`{s1,s2,...}`. If a subscript is the colon character
which stands for all elements along the corresponding dimensions, it is represented
with the string `':'` in `s.subs`.

When an assignment is made to a subscripted part of an object in
a statement like `A{s}=B`, LME executes `A=subsasgn(A,s,B)`,
where `subsasgn` is a method of the class of variable `A`
and `s` is a structure with two fields: `s.type` which
is `'{}'`, and `s.subs` which is the list containing the single
subscript `{s}`.

When an assignment is made to the field of an object in
a statement like `A.f=B`, LME executes `A=subsasgn(A,s,B)`,
where `s` is a structure with two fields: `s.type` which
is `'.'`, and `s.subs` which is the name of the field
(`'f'` in this case).

While the primary purpose of `subsasgn` is to permit the
use of subscripts with objects, a built-in implementation of `subsasgn` is
provided for arrays when `s.type` is `'()'`, for lists and
cell arrays when `s.type` is a list or a cell array, and for structures
when `s.type` is `'.'`. In that case, the second argument
`s` can be reduced to the list of subscripts or the field name;
and a single subscripts can be given directly instead of a list of length 1.

#### Examples

A = [1,2;3,4]; subsasgn(A, {type='()',subs={1,':'}}, 999) 999 999 3 4 subsasgn(A, {type='()',subs={':',1}}, []) 2 4

Same result when the indices are given directly as the second argument:

subsasgn(A, {1,':'}, 999) 999 999 3 4 s = {a=2, b=1:5}; subsasgn(s, 'b', 'abc') a: 2 b: 'abc'

#### See also

Operator `()`,
operator `{}`,
`subsref`,
`beginning`,
`end`

### subsref

Reference to a part of an array, list, or structure.

#### Syntax

B = subsref(A, s)

#### Description

When an object variable is subscripted in an expression
like `A(s1,s2,...)`, LME evaluates `subsref(A,s)`,
where `subsref` is a method of the class of variable `A`
and `s` is a structure with two fields: `s.type` which
is `'()'`, and `s.subs` which is the list of subscripts
`{s1,s2,...}`. If a subscript is the colon character
which stands for all elements along the corresponding dimensions, it is represented
with the string `':'` in `s.subs`.

When an object variable is subscripted in an expression
like `A{s}`, LME evaluates `subsref(A,s)`,
where `subsref` is a method of the class of variable `A`
and `s` is a structure with two fields: `s.type` which
is `'{}'`, and `s.subs` which is the list containing the single
subscript `{s}`.

When the field of an object variable is retrieved in
an expression like `A.f`, LME executes `subsref(A,s)`,
where `s` is a structure with two fields: `s.type` which
is `'.'`, and `s.subs` which is the name of the field
(`'f'` in this case).

While the primary purpose of `subsref` is to permit the
use of subscripts with objects, a built-in implementation of `subsref` is
provided for arrays when `s.type` is `'()'`, for lists
when `s.type` is `'{}'`, and for structures when
`s.type` is `'.'`. In that case, the second argument
`s` can be reduced to the list of subscripts or the field name;
and a single subscripts can be given directly instead of a list of length 1.

#### Examples

A = [1,2;3,4]; subsref(A, {type='()',subs={1,':'}}) 1 2

Same result when the indices are given directly as the second argument:

subsref(A, {1,':'}) 1 2 s = {a='abc', b=1:5}; subsref(s, 'b') 1 2 3 4 5