Navigation:  Language Reference > Statements >


Previous pageReturn to chapter overviewNext page


toMatlab(<20-sim variable>)

toMatlab(<20-sim variable>, 'matlab variable name')


The toMatlab statement allows you to pass a variable from 20-sim to Matlab.


The first argument is the name of the 20-sim variable to send to Matlab (source).

This 20-sim variable can be a scalar variable, a vector or a matrix.

The second (optional) argument 'matlab variable name' is the name of the corresponding matlab variable (destination).


When the 'matlab variable name' argument is omitted, 20-sim will use the full hierarchical name of the 20-sim variable.

A 20-sim variable named Controller\x becomes in Matlab: Controller_x


The corresponding fromMatlab statement allows you to retrieve a variable from Matlab.


// this example shows how a variable can be transferred to matlab and from matlab during simulation


 real x;

 real y;

 real vector[2];

 real matrix[2,2];


// at the start of the simulation


 // create an empty array a

 doMatlab ('a = []; ');

 // create a variable

 doMatlab ('b=0; ');

 // create a vector

 doMatlab ('vector=[1 2];');

 // create a matrix

 doMatlab ('matrix=[1 2; 3 4];');


// during simulation


 // calculate x

 x = sin (time);

 // send it to Matlab

 toMatlab (x,'x');

 // and add it to array a

 doMatlab ('a = [ a x ]; ');

 // in Matlab add 2 to x

 doMatlab ('b = x + 2;');

 // read matlab 'b' into 20-sim variable 'y'

 fromMatlab(y, 'b');

 // read matlab 'vector' into 20-sim variable 'vector'

 fromMatlab(vector, 'vector');

 // read matlab 'matrix' into 20-sim variable 'matrix'

 fromMatlab(matrix, 'matrix');


// at the end of the simulation


 // plot the resulting array in Matlab

 doMatlab (' plot (a); ');