MATLAB Answers

How to run a python code on simulink?

486 views (last 30 days)
I am trying do some image processing and have codes written in python. I am trying to make it run on matlab/simulink. Is there a simulink block that can run python codes? Something like I can give an input to the block, it will run the python code and give the output.

  0 Comments

Sign in to comment.

Accepted Answer

Nick Choi
Nick Choi on 6 Oct 2017
You could try using a MATLAB function block that contains MATLAB code to call the Python code.
This documentation link provides an example of how to integrate a MATLAB function block into a Simulink model,
and this documentation link contains useful information on how to use Python code with MATLAB: https://www.mathworks.com/help/matlab/getting-started-with-python.html
One thing to note is that not all MATLAB functionalities will be supported for code generation so you may need to use 'coder.extrinsic' in the MATLAB function block. Additional information on 'coder.extrinsic' can be found here:

  5 Comments

Show 2 older comments
Henry Kotze
Henry Kotze on 19 Mar 2019
What you need to do is: Create a matlab function (Not a simulink Matlab Function block) where you call your Python script. Remember to convert them to the correct types and ensure that the matlab function is able to see the python script.
Then from simulink you use the Interpreted Matlab Function block in simulink to call your function.
Elif Ecem Bas
Elif Ecem Bas on 4 Mar 2020
Does this work in Simulink Real-Time machine? If not do you suggest any other solutions?
Lode De Herdt
Lode De Herdt on 11 May 2020
It probably won't work in a real-time machine because this function is not supported for code generation. A Simulink model that uses this cannot be translated to C code which is usually what is needed to run it on a real-time machine.

Sign in to comment.

More Answers (1)

Lode De Herdt
Lode De Herdt on 24 Mar 2020
Yes, this is possible to do. A very simple example of Python code running in Simulink using the MATLAB Function block:
The MATLAB Function block refers to this code:
function out = RunPython(in1, in2)
out = 0; % Has to be preassigned, otherwise Simulink throws an error
coder.extrinsic('py.Example.test') % Python functions have to be run extrinsically, meaning no C code generated
out = py.Example.test(in1,in2);
end
And the Python code (in Example.py file) is:
def test(in1,in2):
out = in1+in2
return out

  5 Comments

Show 2 older comments
Sean de Wolski
Sean de Wolski on 11 May 2020
I find it's better to package all of the python functionality into another MATLAB function and then flag that function as extrinsic.
function out = MyBlock(in1, in2)
out = 0; % Has to be preassigned, otherwise Simulink throws an error
coder.extrinsic('usePython') % Python functions have to be run extrinsically, meaning no C code generated
out = usePython(in1, in2);
end
Then
function out = usePython(in1, in2)
out = py.math.remainder(in1, in2);
end
This was everything you do with python is outside of the scope of code generation rathen than just one method.
Grace Connors
Grace Connors on 17 Jun 2020
Is it possible to add back in the python functions once the code has been generated? What if they are integral to running on a real-time machine? (As in, you're using ML predictions from python because MATLAB doesn't support the latest keras update).
Sean de Wolski
Sean de Wolski on 18 Jun 2020
Grace, that warrants it's own question. I suspect you'd need to write a C function to call Python and then use coder.ceval inside the MATLAB function block but there may be a more efficient way. have you looked at the keras importers for MATLAB which may allow you to import the model and generate code from it?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!