Matsim: Build Simulink models with Matlab

Have you ever tried to build a Simulink model from a Matlab script? Yes? I feel your pain.

The standard Simulink API is powerful but cumbersome, as it needs you to manually specify each block position and connections. This is what is needed for creating and connecting 3 blocks:

Quite lengthy! Also with such code is not apparent what the resulting model will be.

>> Introducing Matsim

The Matsim library (un)officially changes this. The library abstracts from the Simulink API and provides a sleek and intuitive interface for building Simulink models programmatically.

The Matsim way

Let’s see how to build the same model with the Matsim library.

Yeah, that’s it! In Matsim each Simulink block is wrapped in a Matlab class. Every time you create an instance of a class, the corresponding Simulink block is added to the model.

Matsim translates common operations to equivalent Simulink blocks. So you can “Add (+)”, “Subtract (-)”, “Multiply (.*)”, “Concatenate ([])”, … blocks, and Matsim will generate the equivalent model. a+b will be translated as an “Add” block, [a,b] as a Mux block and so on.

With Matsim there’s no need to specify the blocks positions on the Simulink model! Matsim uses GraphViz to automatically generate a block layout. To position and connect the blocks already in the model, call simlayout() :

sys = load_system('my_model');
...
% Create blocks
...
% Connect and layout the model
simlayout(sys)

Matsim is still in an early stage and not all blocks are supported. I’ve been working on this tool for a few months. I would love to hear any and all feedback from the Medium community and MATLAB folks!

Checkout the project on Github.