MATLAB Answers

Simulink Compiler with Matlab Compiler SDK to get .NET DLL

7 views (last 30 days)
NVK7 on 14 Apr 2021
Commented: Kojiro Saito on 20 Apr 2021
Hi Folks,
The problem I am trying to solve involves getting my Simulink/Simscape model into a DLL so that it can be called within the .NET framework.
I am aware that there are solutions on this forum which involve the Embedded Compiler and Matlab Coder but I was hoping to find a solution that sppecifically uses the Simulink Compiler along with the Matlab Compiler SDK. There is a example of using Matlab Compiler along with Matlab Compiler SDK to generate a DLL which can be called from a .NET framework ( and I am looking for something similar with Simulink, as the pipeflow seems to be easier. I believe that the level of complexity of my model would be better suited for a shared library based solution.
The toolbox summary overview on the Simulink Compiler homepage ( seems to imply that it can be done. It says in the section "Enterprise Applications" that "With MATLAB Compiler SDK you can package your Simulink simulation into a language-specific software component such as a C/C++ shared library, Java JAR, Python package, or .NET DLL, which can then be invoked from an enterprise application."
However I could not find any example of how this could be done, specifically with the Simulink Compiler - Matlab Compiler SDK coupling.
I was hoping that someone in the community might has some ideas about this particular pipeflow and perhaps could provide some pointers on how it could be done. While I do have a decent background in Matlab/Simulink, I just started using the Compiler Toolbox system recently and thus any help in figuring out this workflow would be much appreciated.
Thanks in advance,

Accepted Answer

Kojiro Saito
Kojiro Saito on 16 Apr 2021
Here is a sample for deploying Simulink model to .NET dll using MATLAB Compiler SDK & Simulink Compiler.
First, create MATLAB function which uses Simulink.SimulationInput and sim commands.
For example, this is a sample based on this example.
function [t, data] = deployParameterTuning(mb)
% [t, data] = deployParameterTuning(1000)
if ischar(mb) || isstring(mb)
mb = str2double(mb);
if isnan(mb) || ~isa(mb, 'double') || ~isscalar(mb)
disp('The value of mb given to deployParameterTuning must be a double scalar or a string or character that can be converted to a double scalar');
in = Simulink.SimulationInput('sldemo_suspn_3dof');
in = in.setVariable('Mb', mb);
in = simulink.compiler.configureForDeployment(in);
out = sim(in);
t = out.logsout{1}.Values.Time;
data = out.logsout{1}.Values.Data;
Then, compile the m script as .NET Assembly using Library Compiler. Click "Package".
You will get a .NET dll file in deployParameterTuning\for_redistribution_files_only.
In Visual Studio, create C# console application.
In this case, solution name is dotNetSlTest.sln and add two references. First is MWArray.dll from (MATLAB_INSTALL\toolbox\dotnetbuilder\bin\win64\v4.0) and the second is deployParameterTuning.dll you just compiled from above .m code.
Then, write the following C# code.
using System;
using deployParameterTuning;
using MathWorks.MATLAB.NET.Arrays;
namespace dotNetSlTest
class Program
static void Main(string[] args)
Class1 obj = null;
MWNumericArray t = null;
MWNumericArray data = null;
MWArray[] result = null;
// Because class instantiation and method invocation make their exceptions at run time,
// you should enclose your code in a try-catch block to handle errors.
// Instantiate your component class.
obj = new Class1();
// Invoke your component
result = obj.deployParameterTuning(2, args[0]);
// Extract the Magic Square you created from the first index of result
t = (MWNumericArray)result[0];
data = (MWNumericArray)result[1];
// print the output.
In Visual Studio, change the target CPU to x64 from Any CPU, then build the solution.
Once you obtain the .NET exe file, run the following.
dotNetSlTest.exe 1000
For applying this approach to you actual Simulink model and MATLAB codes, please check this document whether the MATLAB functions, Simulink blocks and Toolbox which you're using are supported in MATLAB Compiler / Simulink Compiler.

More Answers (1)

NVK7 on 20 Apr 2021
Hi Kojiro,
Thanks for the detailed instructions. This is precisely what I was looking for!! Appreciate the prompt response and all the help and effort that went into this.
I will try out the example that you have provided which should help me in understanding the workflow in detail. However I hope you wouldn't mind if I update this post if I hit any serious roadblocks. Hopefully it shouldn't happen since your instructions are quite clear and I think you have provided everything that is necessary but I always keep my fingers crossed when it comes to coding!!
Thanks again for all the help,
  1 Comment
Kojiro Saito
Kojiro Saito on 20 Apr 2021
Of course!
If you have furthrer question, please post your comment inside my answer, not in Answer form.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!