Tomasz

Would you provide some examples of using StaticVariable with a Matrix

The concept of Static Variables and Matrix are two very different things.

Can you be more specific?

Static Variables are stored (life either limited to runtime or persistent ) but primarily "saved" to a state where they can be accessed from multiple independent running AFL.

You can read more on the scope of variables.

Any AB Data type can be Set as a static variable (String, Float, Matrix, Array)

Matrix on the other hand is a data type loosely similar to an array but with user-defined size/dimensions (ie. rows & columns) and Matrix related functions (operations)

It is not much difference to using static variables with other types such as array, string, number.

One thing to note is that if static variable call returns **empty** then type of static variable (called via StaticVarGet) is **not** matrix but then type of static variable is number (NULL -> -1e10). So you would need to check for type being matrix first and then doing your matrix operations.

Otherwise (if not checking for type first) AmiBroker would return error message "Error 5"

So example (avoiding such error message):

```
/// Static variables and matrices
/// @link https://forum.amibroker.com/t/staticvariables-with-matrix/11241/3
/// storing matrix to static variable
if ( ParamTrigger( "Create static mat", "CLICK HERE" ) ) {
mat = MxFromString( "[1,2,3;4,5,6;7,8,9]" );
// storing matrix to static variable after trigger returning true
StaticVarSet( "Sample_static_mat", mat );
}
/// static_mat may be type number or matrix!!
/// if static var is empty then it is type "number" (NULL -> -1e10)
static_mat = StaticVarGet( "Sample_static_mat" );
/// so to avoid "Error 5" message
/// we need to check for type of static variable
/// using typeof operator
if ( typeof(static_mat) == "matrix" ) {
_TRACE( "Matrix does exist." );
// then do some matrix stuff
mat = static_mat;
printf( MxToString(mat) );
rownum = MxGetSize(mat, 0);
colnum = MxGetSize(mat, 1);
// etc.
} else {
_TRACE( "Matrix does not exist!" );
// or error message
//Error( "Matrix does not exist!" );
}
```

`typeof()`

is of course very good way to handle that, but there is also another method to handle non-existing static variables without need to resort to typeof operator. This method is simple `if()`

statement:

```
x = StaticVarGet("svar");
if( x ) // works with matrices and numbers
{
// at this place we know that x exists (and is non-zero/non-null)
// but keep in mind that "x" may be of type different than matrix (we did not check it)
printf("Matrix %s\n", MxToString( x ) );
}
else
{
StaticVarSet( "svar", Matrix( 2, 2, 1 ) );
}
```

Also IsNull() function accepts matrices and numbers and would return False if passed argument is a matrix or not-null number.

As per ReleaseNotes.html document:

matrix identifier can now be used in if-else statement. Such condition checks whenever very first element of matrix [0][0] is NOT NULL. This is useful for checking output of functions like MxInverse/MxSolve that would return NULL in all cells if matrix is singular, so:

```
m = Matrix( 10, 10, 0 );
// do something with matrix
if( m )
{
// some code
}
```

is equivalent to

```
if( NOT IsNull( m[ 0 ][ 0 ] ) )
{
// some code
}
```

In the latest version, it is mentioned that static variables can be used as regular variables . no need to user set and get functions. can you please share example how to define static matrix variables as per the Ami broker 6.30 version?

============

The static keyword allows to declare

Syntax:

**static** *identifier;*

**static** *identifier1, identifier2, identifier3, ...;*

The **static** keyword declares identifier as static variable

# Declared static variables can and should be used as 'regular' variables, no need to call functions (Static*).

It is described in manual. And example usage is already in the quoted paragraph.