Access Keys:
Skip to content (Access Key - 0)
Agilent Technologies

MATLAB Cosimulation

The MATLAB_Cosim part provides an interface between SystemVue and MATLAB®, a numeric computation and visualization environment from The MathWorks, Inc.

SystemVue handles the conversion of data to and from MATLAB. Since MATLAB cosimulation involves multiple environments and associated inter-process communication, the installation and pre-simulation configuration must be precise and correct for the infrastructure to work as expected. To ensure proper operation, the instructions provided in Setting Up MATLAB Cosimulation must be followed.

MATLAB cosimulation using multiple MATLAB_Cosim parts does not work in multithreaded simulations. If you are using multiple MATLAB_Cosim parts, the multithreaded simulation option will be automatically overridden and the simulation will run in a single thread.

Supported MATLAB Versions

MATLAB Cosimulation requires MATLAB and supports MATLAB version 7.7 (Release 2008b).

There is also a good chance that other MATLAB versions would work in SystemVue as well.

Setting Up MATLAB Cosimulation

MATLAB must be configured correctly before using cosimulation. If MATLAB_Cosim is run and MATLAB is not configured correctly, SystemVue will report an error.

You should be able to manually launch MATLAB from the Windows Command Prompt. If MATLAB does not start that way, MATLAB_Cosim will not work either. This may be due to the fact that your PATH variable exceeds the Windows limitation, in which case you may want to reduce it or create a batch script with a reduced PATH variable to launch SystemVue . Alternatively you may want to just copy all of the MATLAB DLLs (libeng.dll, libut.dll, etc.) from its bin\win32 folder to the SystemVue bin folder (but remember to replace them after an upgrade).

In addition, MATLAB COM server must be properly registered.

For most Windows users, SystemVue MATLAB cosimulation will work as expected when MATLAB and SystemVue are installed.
Typically, the MATLAB installer registers the COM components in the Windows registry.

To manually register COM components run

matlab /regserver

This may be necessary if you have multiple versions of MATLAB on your system and SystemVue MATLAB cosimulation fails with an error "Matlab could not be invoked".

Simulating with MATLAB

The MATLAB interpreter's working directory is set to the ScriptDirectory parameter, if it is given. Any custom MATLAB models will be searched there, and any output files will be written there. If you leave the ScriptDirectory parameter blank, MATLAB_Cosim will use the SystemVue workspace folder as its working directory.

MATLAB will search for scripts in the folders in your MATLAB path. Normally your MATLAB scripts should be placed either in the folder with the workspace file using them, or in your personal MATLAB folder (usually found under My Documents). You can also add more folders to your MATLAB path using Set Path... dialog in MATLAB.

Writing Functions for MATLAB_Cosim

There are several ways in which MATLAB commands can be specified in the MATLAB_Cosim in the MatlabFunction parameter.

If only a MATLAB function name is given for this parameter, the function is applied to the inputs in order. The function's outputs are sent to the model's outputs.

For example, specifying eig means to perform the eigendecomposition of the input. The function will be called to produce one or two outputs, according to how many output ports there are. If there is a mismatch in the number of inputs and outputs between the MATLAB_Cosim part and the MATLAB function, then an error will be reported by MATLAB.

You may also explicitly specify how the inputs are to be passed to a MATLAB function and how the outputs are taken from the MATLAB function. For example, consider a two-input, two-output MATLAB_Cosim part to perform a generalized eigendecomposition. The command

[output#1, output#2] = eig( input#1, input#2 );

says to perform the generalized eigendecomposition on the two input matrices, place the generalized eigenvectors on output#2, and the eigenvalues (as a diagonal matrix) on output#1. Before this command is sent to MATLAB, all "#" characters are replaced with the underscore character "_" because "#" is illegal in a MATLAB variable name.

The MATLAB_Cosim part also allows a sequence of commands to be evaluated. Continuing with the previous example, we can plot the eigenvalues on a graph after taking the generalized eigendecomposition:

[output#1, output#2] = eig( input#1, input#2 ); plot( output#1 );

When entering such a collection of commands in SystemVue , both commands appear on the same line without a new line after the semicolon. In this way, very complicated MATLAB commands can be built up.

The MatlabSetup and MatlabWrapUp parameters are called during the model's begin and wrap-up procedures. During each of these procedures, data is not passed into or out of the model.

Because the same MATLAB interpreter is used for the entire simulation, variables are preserved from iteration to iteration. For example, the output of a MATLAB_Cosim part with settings:

MatlabSetUp = 'x=ones(2,1);
MatlabFunction = 'output#1=x(2)/x(1); x=[x(2),sum(x)];

will converge on the golden mean.

Using MATLAB_Cosim as a Source or Sink

The input to MATLAB_Cosim is optional, so it can be directly used as a simulation source.

The output is not optional, it must be connected to a simulation sink. If the MATLAB code produces no output, a dummy output needs to be created. Just add "ouput#1=0" at the end of your MATLAB command, e.g.

MatlabSetUp = 'hold on;
MatlabFunction = 'plot(input#1,input#2); output#1=0;
MatlabWrapUp = 'pause(20);

Passing Parameters to MATLAB_Cosim

MATLAB commands are executed in the MATLAB environment and thus are not directly aware of the parameters existing in your SystemVue workspace. Therefore, something like MatlabSetUp = 'm_script_param=SystemVue_param; will not work. To pass a parameter to MATLAB command you need to use equations to create a string containing the parameter value. For instance, in the Equations tab create

matlab_setup_str = ['m_script_param=' num2str(SystemVue_param)];

and then assign that string variable directly to MatlabSetUp:

MatlabSetUp = matlab_setup_str

See Equations for more information about SystemVue equations.

Hiding MATLAB Code

If you don't want to share your MATLAB IP with other users, you can generate MATLAB p-code files from your m-code by using the MATLAB command pcode.

For instance, if your MATLAB code is in the file mycode.m in the directory of your SystemVue workspace, you need to open MATLAB and in the MATLAB Command Window use cd command to go to that directory and execute

pcode mycode.m

You will find mycode.p in the same directory. When you run the simulation, mycode.p will be executed instead of mycode.m, and you can remove mycode.m from your workspace directory, leaving only mycode.p. The format is a non-readable binary, so your m-code is not visible to other users.