fromMatlab(<20-sim variable>)
fromMatlab(<20-sim variable>, 'matlab variable name')
The fromMatlab statement allows you to retrieve a variable from Matlab and store it into a 20-sim variable.
The first argument is the name of the 20-sim variable to use as storage (destination).
This 20-sim variable can be a scalar variable, a vector or a matrix and should match the size used at the Matlab side.
The second (optional) argument 'matlab variable name' is the name of the Matlab variable to retrieve (source).
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 requests from Matlab: Controller_x
The corresponding toMatlab statement allows you to pass a variable from 20-sim to Matlab.
// this example shows how a variable can be transferred to matlab and from matlab during simulation
variables
real x;
real y;
real vector[2];
real matrix[2,2];
// at the start of the simulation
initialequations
// 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
equations
// 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
finalequations
// plot the resulting array in Matlab
doMatlab (' plot (a); ');