{"id":9495,"date":"2020-08-05T16:20:43","date_gmt":"2020-08-05T20:20:43","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=9495"},"modified":"2020-08-05T16:20:43","modified_gmt":"2020-08-05T20:20:43","slug":"getting-started-with-simulink-compiler","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2020\/08\/05\/getting-started-with-simulink-compiler\/","title":{"rendered":"Getting Started with Simulink Compiler"},"content":{"rendered":"<p>In MATLAB R2020a, we introduced a new product: <strong><a href=\"https:\/\/www.mathworks.com\/products\/simulink-compiler.html\">Simulink Compiler<\/a><\/strong><\/p>\n<p>In this post, I am sharing the basic information you will need to get started with Simulink Compiler.<\/p>\n<p><strong>What is Simulink compiler?<\/strong><\/p>\n<p>To understand how Simulink Compiler works, it's a good idea to begin by getting familiar with <a href=\"https:\/\/www.mathworks.com\/products\/compiler.html\">MATLAB Compiler<\/a>. With MATLAB Compiler, you can take pretty much any MATLAB code and turn it into a standalone application that your end-users will be able to run royalty-free using <a href=\"https:\/\/www.mathworks.com\/products\/compiler\/matlab-runtime.html\">MATLAB Runtime<\/a>.<\/p>\n<p>Depending on your needs, the final standalone application can take various forms and be shared in multiple ways. Here are a few examples:<\/p>\n<ul>\n<li>Standalone executable: Give your end user an executable that they can run on their machine without a MATLAB installation.<\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/compiler\/web-apps.html\">Web Apps<\/a>: Create a MATLAB App using App Designer and package it as a web app. Using the MATLAB Web App Server, you will be able to host it and simply share a hyperlink to your end users who will use the compiled app in a web browser.<\/li>\n<li>Software components: Use <a href=\"https:\/\/www.mathworks.com\/products\/matlab-compiler-sdk.html\">MATLAB Compiler SDK<\/a> to package a MATLAB program into software components to be integrated with other programming languages like C\/C++ shared libraries, Microsoft\u00ae .NET assemblies, Java\u00ae classes, and Python\u00ae packages. Large-scale deployment to enterprise systems is supported through\u202f<a href=\"https:\/\/www.mathworks.com\/products\/matlab-production-server.html\">MATLAB Production Server<\/a>.<\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/compiler\/create-excel-add-ins-1.html\">Microsoft Excel Add-ins<\/a>: Give Excel users in your company the power of MATLAB and Simulink through Excel.<\/li>\n<p>Take a look at the product pages for <a href=\"https:\/\/www.mathworks.com\/products\/compiler.html\">MATLAB Compiler<\/a> and <a href=\"https:\/\/www.mathworks.com\/products\/simulink-compiler.html\">Simulink Compiler<\/a> for more details on all the capabilities.<\/p>\n<p>If you ask me to summarize in a few words the new capabilities that Simulink Compiler adds on top of MATLAB Compiler, my answer would be that with Simulink Compiler the deployed MATLAB code can now use the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/simulink\/slref\/sim.html\">sim<\/a> command.<\/p>\n<p><strong>A Simple Example<\/strong><\/p>\n<p>When describing new features, I like to go through a simple example. For this case, let's get started with a simple mass-spring-damper, similar to the starting point of the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/simulink\/ug\/modeling-workflow.html\">Basic Modeling Workflow<\/a> example we added to the Simulink documentation a few releases ago:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleMdl.png\" alt=\"Simple test model\" \/><\/p>\n<p>For convenience, I defined default values for the parameters and input signal in the model workspace (This is not necessary, but I find it useful to have default values in the Model Workspace. That way I can test the model as I am developing it without having any variables defined in the base workspace):<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleMdlWorkspace.png\" alt=\"Model workspace\" \/><\/p>\n<p>I can simulate the model with a simple function like this one:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleSim.png\" alt=\"MATLAB Function simulating the model\" \/><\/p>\n<p>If you would like to deploy this function using Simulink Compiler, you only need to add one line of code to configure the SimulationInput object for deployment.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/configureForDeployment.png\" alt=\"Configuring for deployment\" \/><\/p>\n<p>What <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/slcompiler\/ref\/simulink.compiler.configurefordeployment.html\">simulink.compiler.configureForDeployment<\/a> does can be summarized as basically one thing: it sets the simulation mode to Rapid Accelerator. You can read more on this requirement and its implications <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/slcompiler\/ug\/rapid-accelerator-dependencies.html\">here<\/a>.<\/p>\n<p>Finally, you generate the executable using the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/compiler\/mcc.html\">mcc<\/a> function.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleMCC.png\" alt=\"Compiling the executable\" \/><\/p>\n<p>Once the executable is ready, you can launch it from a DOS prompt or by double-clicking on it in Windows Explorer.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleEXE1.png\" alt=\"Launching the executable\" \/><\/p>\n<p><strong>Tuning Parameters and Input signals<\/strong><\/p>\n<p>The executable we just created only simulates the model with the same input signals and the same parameter values as used when compiling it. For more realistic applications, you probably want the end-user to be able to specify new values and observe the impact on the simulation results.<\/p>\n<p>This can be done in many ways; here is an example where I made 3 block parameters input arguments to the function and applied them to the simulation using the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/simulink\/slref\/setvariable.html\">setVariable<\/a> method of the SimulationInput object.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleSimWithParameters.png\" alt=\"Tuning Parameters\" \/><\/p>\n<p>Once deployed, I can then pass new values to the executable:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleEXE.png\" alt=\"Launching the executable\" \/><\/p>\n<p>Here is a different example where I read a new input signal from CSV file and specify it using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/simulink\/slref\/setexternalinput.html\">setExternalInput<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleSimWithInputs.png\" alt=\"Tuning Input Signal\" \/><\/p>\n<p>As you can see, tuning parameters and input signals is not more complicated for a deployed application then it is for code running in a MATLAB installation.<\/p>\n<p><strong>Deploying an App with App Designer<\/strong><\/p>\n<p>Now that we have seen the basics of configuring a SimulationInput object for deployment and tuning parameters and input signals, let's move to something more interesting: Deploying a MATLAB App simulating a Simulink model.<\/p>\n<p>To get started, I launched <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/matlab\/ref\/appdesigner.html\">appdesigner<\/a> and created a simple app with 3 sliders, one push button and one axes:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleApp1.png\" alt=\"Simple app\" \/><\/p>\n<p>I created a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/matlab\/ref\/matlab.ui.control.button-properties.html#d120e1409116\">Button Pushed Callback<\/a> and added the following code, which is very similar to one of the above example:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/buttonCallback.png\" alt=\"Pushed button callback\" \/><\/p>\n<p>With that, we are ready to package the app as an executable.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleAppEXE.png\" alt=\"Packaging an executable\" \/><\/p>\n<p>Now we have an app that can be run outside of MATLAB.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleAppEXE.gif\" alt=\"the executable\" \/><\/p>\n<p>If you prefer, you can also deploy your app as a Web App.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleWebApp.png\" alt=\"Packaging a Web app\" \/><\/p>\n<p>That way, once you have a <a href=\"https:\/\/www.mathworks.com\/products\/matlab-web-app-server.html\">Web App Server<\/a> running, your end-users will be able to run the app from a web browser.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleWebAppDeployed.png\" alt=\"Using a Web app in a web browser\" \/><\/p>\n<p><strong>What's Next?<\/strong><\/p>\n<p>In this blog post, we went through the basics of Simulink Compiler. I recommend going through the documentation example for <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/slcompiler\/ug\/deploy-a-simulation-with-simulink-compiler.html\">Deploying an App Designer Simulation with Simulink Compiler<\/a>. This will teach you a few more tricks to improve the usability of your app.<\/p>\n<p>In my next post, I will describe how I built an app for the <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2020\/03\/23\/covid-19-simulating-exponential-spread-in-simulink\">COVID-19 exponential spread simulator I published a few months ago<\/a>. This will allow me to cover more advanced tips and tricks for deploying more complex apps with Simulink Compiler.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2020Q3\/simpleWebAppDeployed.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>In MATLAB R2020a, we introduced a new product: Simulink Compiler<br \/>\nIn this post, I am sharing the basic information you will need to get started with Simulink Compiler.<br \/>\nWhat is Simulink compiler?<br \/>\nTo... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2020\/08\/05\/getting-started-with-simulink-compiler\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[602],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/9495"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=9495"}],"version-history":[{"count":37,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/9495\/revisions"}],"predecessor-version":[{"id":9698,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/9495\/revisions\/9698"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=9495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=9495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=9495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}