Nontas' Blog

Simulink+Jenkins: Model verification & code generation

creating a Jenkins job for automated model verification and code generation of Simulink models


In one of the recent projects I have worked on, we had to create Simulink models, verify them for correctness, and then generate C++ code, which would be executed on a different machine.

In case you are working alone, or you are the only one working on the models, this is not a big problem; you engage your creativity to create the models, you verify them, and then you generate code. Simple as that. However, if you are working together with five more people on the same models, it is not so efficient to gather everyone around a screen and “work together”. Instead, it would be better if everyone worked on a specific part of the model and each member’s effort is committed to a git repository. (But of course, this is no big news)

The problem

Suppose then that you have your repository ready to accept your models and everybody is working hard to create the desired models. But then what? How can you automatically verify that each commit did not break the correctness of the models? Moreover, (just to make it a bit more interesting) how can you generate C++ code in an automated way after the verification of the models? Well, that’s what we are going to see here.

The solution

I am going to present the solution we gave to this problem.
Disclaimer: Needless to say that there might be a better solution out there; this was just our solution. If you, however, have a better solution to propose, please let me know in the comments section after this post.

What we will need

Our configuration in this project consisted of the following (among others):

  • PCs running Windows and the full installation Matlab R2016a
  • One Jenkins PC running Ubuntu Server
  • One Jenkins node PC running Ubuntu Desktop and the full installation of Matlab R2016a for linux


  1. For us it was not a problem to get the full installations of Matlab for Windows and Linux (thanks TU/e!), but if you cannot obtain it, you should at least have the Toolbox Simulink Coder (more info).
  2. Our Jenkins PC was not that powerful to run Matlab, therefore we got another PC and made it a Jenkins node. Whenever Jenkins wanted to execute a Matlab job, this was done on this node and the results were transferred back to Jenkins.

Automating Matlab

The first step towards the ultimate automation of this solution, is to create a Matlab script that will open our models and invoke Simulink’s build procedure, which verifies the model, and if everything is fine code is generated.
A script like this, looks like:

	warning('Error while building Model1');
	warning('Error while building Model2');

One thing that needs to be mentioned here: if you do not include the command set_param('Model1','LaunchReport','off');, then an annoying pop-up window from Matlab will open every time your model is built. This was rather annoying for us and, therefore, we disabled it.

You can include this file in your repository, or just place it in the home folder of the Jenkins node machine and name it autobuild.m.

Creating a Jenkins job

The last step here is to create a job on your Jenkins server.
Be careful to select the following:

  • Restrict where this project can be run, where you should select your Jenkins node machine.
  • In Source Code Management you should configure your repository/ies.
  • In Build Triggers you should select what will be the trigger(s) for this job.
  • In Build you need to specify the build actions. For this case, first we need to get the autobuild.m and then invoke Matlab.

The Build section should look like:

cp -f /home/jenkins/autobuild.m /home/jenkins/workspace/REPO_NAME/MODELS_FOLDER/
cd /home/jenkins/workspace/REPO_NAME/MODELS_FOLDER/
matlab -nosplash -nodesktop -noFigureWindows -r autobuild

After you create your job, you should be able to see if your models are correct and get your generated code.

Optionally, you can create a Post-build Action to transfer the generated code to another folder, or to archive it and give it a specific label.