How to run a python code on simulink?

조회 수: 121 (최근 30일)
Nitharshini Sivakumar
Nitharshini Sivakumar 2017년 10월 4일
댓글: PRANESH KUMAR 2022년 3월 29일
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
faazila fathima
faazila fathima 2021년 7월 9일
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'.

댓글을 달려면 로그인하십시오.

채택된 답변

Nick Choi
Nick Choi 2017년 10월 6일
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
Sean de Wolski
Sean de Wolski 2021년 3월 17일
편집: Sean de Wolski 2021년 3월 17일
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 2022년 3월 29일
I want to run python code which is inside raspberry pi and simulink on pc.how to go about it

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Lode De Herdt
Lode De Herdt 2020년 3월 24일
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
Sean de Wolski
Sean de Wolski 2021년 5월 17일
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 2021년 7월 14일
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'.

댓글을 달려면 로그인하십시오.


Weiwu Li
Weiwu Li 2021년 10월 19일
편집: Weiwu Li 2021년 10월 19일
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
Bartosz Soltowski
Bartosz Soltowski 2021년 12월 7일
편집: Bartosz Soltowski 2021년 12월 7일
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 2021년 12월 10일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by