{"id":19130142,"url":"https://github.com/mathworks/call-simulink-from-python","last_synced_at":"2025-10-13T16:38:02.456Z","repository":{"id":142438479,"uuid":"613480493","full_name":"mathworks/Call-Simulink-from-Python","owner":"mathworks","description":"Examples to show the two options to simulate a Simulink model from Python","archived":false,"fork":false,"pushed_at":"2023-03-17T21:03:42.000Z","size":837,"stargazers_count":23,"open_issues_count":1,"forks_count":4,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-06T00:53:32.871Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"MATLAB","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mathworks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-03-13T16:45:08.000Z","updated_at":"2025-04-26T01:15:09.000Z","dependencies_parsed_at":"2023-03-21T20:32:39.522Z","dependency_job_id":null,"html_url":"https://github.com/mathworks/Call-Simulink-from-Python","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathworks%2FCall-Simulink-from-Python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathworks%2FCall-Simulink-from-Python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathworks%2FCall-Simulink-from-Python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathworks%2FCall-Simulink-from-Python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mathworks","download_url":"https://codeload.github.com/mathworks/Call-Simulink-from-Python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252601681,"owners_count":21774659,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T06:09:42.037Z","updated_at":"2025-10-13T16:37:57.435Z","avatar_url":"https://github.com/mathworks.png","language":"MATLAB","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simulate a Simulink\u003csup\u003e\u0026reg;\u003c/sup\u003e model from Python\u003csup\u003e\u0026reg;\u003c/sup\u003e \r\n\r\nThis example illustrates two ways to simulate a Simulink model (named \u003ccode\u003ethe_model\u003c/code\u003e) via a wrapper MATLAB\u003csup\u003e\u0026reg;\u003c/sup\u003e function (named \u003ccode\u003esim_the_model\u003c/code\u003e) from Python. \r\n\r\n![the_model](the_model.jpg)\r\n \r\nThe first approach, shown in \u003ccode\u003ecall_sim_the_model_using_matlab_engine.py\u003c/code\u003e, uses the [MATLAB Engine API for\r\nPython](https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html) to call the wrapper function \u003ccode\u003esim_the_model.m\u003c/code\u003e multiple times passing in different parameters and external input signals. \r\n\r\n![results_mle](results_mle2.png)\r\n \r\nThe second approach uses [MATLAB Compiler\r\nSDK](https://www.mathworks.com/help/compiler_sdk/gs/create-a-python-application-with-matlab-code.html)\u003csup\u003e\u0026trade;\u003c/sup\u003e and [Simulink Compiler](https://www.mathworks.com/help/slcompiler/ug/deploy-from-matlab-command-line.html)\u003csup\u003e\u0026trade;\u003c/sup\u003e to first build a Python package around the wrapper function \u003ccode\u003esim_the_model.m\u003c/code\u003e as shown in\r\n\u003ccode\u003ebuild_python_package_around_sim_the_model.m\u003c/code\u003e. We can then call this package to run the warpper function \u003ccode\u003esim_the_model.m\u003c/code\u003e multiple times passing in different parameters and external input signals as show in \u003ccode\u003ecall_sim_the_model_using_matlab_runtime.py\u003c/code\u003e.  \r\n\r\n![results_mrt](results_mrt.png)\r\n\r\nThis example includes the following files:\r\n\r\n* \u003ccode\u003ethe_model.slx\u003c/code\u003e: the Simulink model we will simulate in the example;\r\n* \u003ccode\u003esim_the_model.m\u003c/code\u003e: the wrapper MATLAB function to simulate a Simulink model with the specified parameter and input signal values;\r\n* \u003ccode\u003ecall_sim_the_model.m\u003c/code\u003e: MATLAB script used to call the sim_them_model multiple times in MATLAB with different inputs and parameters; \r\n* \u003ccode\u003eplot_results.m\u003c/code\u003e: MATLAB script used by call_sim_the_model to plot the results;\r\n* \u003ccode\u003ecall_sim_the_model_using_matlab_runtime.py\u003c/code\u003e: Python script to call sim_the_model packaged function multiple times and plot the results;\r\n* \u003ccode\u003ecall_sim_the_model_using_matlab_engine.py\u003c/code\u003e: Python script that uses MATLAB Engine API to call sim_the_model.m multiple time and plot the results;\r\n* \u003ccode\u003eCallingSimFromPython.pptx\u003c/code\u003e: complementary presentation slides describing the demo structure and setup\r\n\r\nThe model \u003ccode\u003ethe_model.slx\u003c/code\u003e and the wrapper MATLAB function \u003ccode\u003esim_the_model.m\u003c/code\u003e illustrate implementation choices that make data marshaling between Python and \u003ccode\u003esim\u003c/code\u003e command in MATLAB relatively straight forward and can be used with any Simulink model. These are:\r\n\r\n* Parameterizing the Simulink model using workspace variables makes it easy run sim with new parameter values passed in from Python.\r\n* Labeling the logged signals in the model with valid identifiers, makes it easy to pack the results into a MATLAB struct and return to Python.\r\n* Extracting the time and data values as numeric vectors from sim command output and returning these to Python makes data marshaling relatively easy. \r\n\r\n\r\n\r\nThis example has been tested with MATLAB R2022b and Python 3.8. The following MathWorks products are needed for using this example:\r\n\r\n* [MATLAB](https://www.mathworks.com/products/matlab.html); \r\n* [Simulink](https://www.mathworks.com/products/simulink.html); \r\n* [MATLAB Compiler\u003csup\u003e\u0026trade;\u003c/sup\u003e](https://www.mathworks.com/products/compiler.html); \r\n* [MATLAB Compiler SDK](https://www.mathworks.com/products/matlab-compiler-sdk.html); \r\n* [Simulink Compiler](https://www.mathworks.com/products/simulink-compiler.html); \r\n \r\n\r\n\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathworks%2Fcall-simulink-from-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathworks%2Fcall-simulink-from-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathworks%2Fcall-simulink-from-python/lists"}