Clear Filters
Clear Filters

Use the code written in PYTHON in MATLAB/Simulink R2015b

3 views (last 30 days)
I am using MATLAB/Simulink version R2015b. Is it possible to incorporate and use code written in Python in a model of MATLAB/Simulink version R2015b? What kind of methods should I do?

Answers (2)

atharva
atharva on 7 Dec 2023
Hey 猛,
I understand that you want to know how to incorporate and use code written in Python in a model of MATLAB/Simulink version R2015b
You can run a Python script from MATLAB using the "system" function. For example, if your Python script was named "file.py", you could run it from MATLAB using the following command:
>> system('python file.py')
See this MATLAB answers post for more information on this workflow.
However, running a Python script from Simulink is not officially supported. MATLAB code can be executed by a Simulink model, but this typically requires the MATLAB code to be supported for code generation, even if you are just simulating the Simulink model. The "system" function is not supported for code generation.
The best potential workaround would be to write a MATLAB function that uses the "system" command, and then use an Interpreted MATLAB Function block to run this MATLAB function in your Simulink model. If this method works, you will not be able to generate code for your model, and the model will likely simulate very slowly. If this method does not work, the best workaround would be to implement the functionality of your Simulink model in MATLAB instead of Simulink, since MATLAB does officially integrate with Python scripts.
You can look at the following documentation to have a better idea-
I hope this helps!

猛
on 10 Dec 2023
Hi atharva,
Thank you very much. This is very useful advice for me. I will give it a try.

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!