How to run a python code on simulink?

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.

1 comentario

faazila fathima
faazila fathima el 9 de Jul. de 2021
sir i tried calling python from matlab, but am unable to do. pls help am getting this error 'Caused by:
Undefined function 'py.te2.c2' for input arguments of type 'double'.

Iniciar sesión para comentar.

 Respuesta aceptada

Nick Choi
Nick Choi el 6 de Oct. de 2017

4 votos

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:

7 comentarios

Nitharshini Sivakumar
Nitharshini Sivakumar el 6 de Oct. de 2017
Thanks a lot. That really helps.
Bikash Sah
Bikash Sah el 22 de Mayo de 2018
Can you please share a sample model that links Python with Simulink. I tried multiple times, but I was not able to.
Henry Kotze
Henry Kotze el 19 de Mzo. de 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 el 4 de Mzo. de 2020
Does this work in Simulink Real-Time machine? If not do you suggest any other solutions?
Lode De Herdt
Lode De Herdt el 11 de Mayo de 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.
Sean de Wolski
Sean de Wolski el 17 de Mzo. de 2021
Editada: Sean de Wolski el 17 de Mzo. de 2021
It is possible to call python code inside of a C S-Function. It's fairly involved and requires connecting a lot of pipes. Please contact if this interests you or ask a new question.
PRANESH KUMAR
PRANESH KUMAR el 29 de Mzo. de 2022
I want to run python code which is inside raspberry pi and simulink on pc.how to go about it

Iniciar sesión para comentar.

Más respuestas (2)

Lode De Herdt
Lode De Herdt el 24 de Mzo. de 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

11 comentarios

Moein Sabounchi
Moein Sabounchi el 9 de Mayo de 2020
I tried this, but I repeatedly receive the error that the function specified in "coder.extrinsic" AKA python function is undefined. I was wondering if you have faced this issue and what is the solution?
Thanks
Lode De Herdt
Lode De Herdt el 11 de Mayo de 2020
Make sure Python commands can run in MATLAB before trying to run it in Simulink. If you run pyenv (or pyversion if you have an older release) in the MATLAB command window, does it return the Python version you're wanting to use? If not, check the documentation on how to set MATLAB's Python interpreter.
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 el 17 de Jun. de 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 el 18 de Jun. de 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?
Quan minhquanccna
Quan minhquanccna el 14 de Mzo. de 2021
Thank you for the example, it works well for me. However, when I change the python function from (out = in1+in2) to (out = in1*in2) for example. I need to close and call Matlab again from Anaconda prompt. Otherwise, MATLAB still understands that is "add" in the code. Can you help me with this?
Quan, please ask this as a new question next time. You need to clear and reload the module:
function reloadLib(libname)
% Reload the Python library
% Necessary if changes are made to .py code
clear classes %#ok<CLCLS>
m = py.importlib.import_module(libname);
py.importlib.reload(m);
Quan minhquanccna
Quan minhquanccna el 20 de Mzo. de 2021
Thank you for your support, Sean.
Hello Lode De Herdt
I have python function where its output is python list how do I get that output?
Sean de Wolski
Sean de Wolski el 17 de Mayo de 2021
K G V,
You could convert the py.list to a cell array c = cell(list) but even that doesn't make much sense to propagate as a Simulink signal. You'll probably want to grab the elements inside of it and determine what to do with them. Can you give more insight into the use case? And probably worth it to create a new question.
faazila fathima
faazila fathima el 14 de Jul. de 2021
sir, i tried exactly what u did.
But when i call the python code in matlab command window, am getting this error. what does this error mean?
Unrecognized function or variable 'in1'.

Iniciar sesión para comentar.

Weiwu Li
Weiwu Li el 19 de Oct. de 2021
Editada: Weiwu Li el 19 de Oct. de 2021

0 votos

You can either use a MATLAB function block or a MATLAB System block to bring Python code into Simulink.
You can refer to the Simulink doc for a simple example:
For a more complex / realistic example (using Python for human detection), you can refer to this file exchange submission:

2 comentarios

Bartosz Soltowski
Bartosz Soltowski el 7 de Dic. de 2021
Editada: Bartosz Soltowski el 7 de Dic. de 2021
Hi,
I managed to integrate Python functions into my Simulink code but I am facing problems while deploying the system into external hardware (Raspberry PI). The Matlab coder does not recognise extrinsic functions. Is there any solution to that problem?
Sean de Wolski
Sean de Wolski el 10 de Dic. de 2021
The solution is to use a C-S function and a custom TLC that tells the MATLAB code generator how to generate C code that can communicate with Python. As I said above, this is doable but really hard and involved. It's like me in English trying to tell someone who speaks Korean how to communicate with someone who speaks Swahili.
In general, it would be far better to take the python things you're doing right now in coder.extrinsic and rewrite them in Simulink/MATLAB/C/C++. Can you describe what the python piece is doing right now and what the data I/O from the MATLAB function to it are?

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 4 de Oct. de 2017

Comentada:

el 29 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by