call python function using matlab function and matlab system block -凯发k8网页登录
this example shows how to call a python® function in simulink® that sorts random numbers using a python® sorting function and two different blocks: the matlab function block and matlab system block. for more on using python in matlab®, see .
matlab supports the reference implementation of python, often called cpython. if you are on a mac or linux® platform, you already have python installed. if you are using windows®, you need to install a distribution, such as those found at . for more information, see .
use python functions in simulink model
this model contains a random number generator that outputs a 1x5 double
containing numbers from 0 to 1, a matlab function and a matlab system block that sorts the numbers, and a manual switch that leads to a display block.
the matlab function block calls the py.sorted
function from python and outputs a 1x5 double
sorted list, as seen below in the code snippet.
function y = fcn(u) % sort values of 'u' coder.extrinsic('py.sorted'); ytmp = py.sorted(u); end
the matlab system block calls the py.sorted
function as part of and outputs a 1x5 double
sorted list as seen below in the code snippet.
function y = stepimpl(~,u) % implement algorithm. calculate y as a function of input u and % discrete states. y = py.sorted(u); y = cellfun(@double,cell(y)); end
running this model using the matlab function block or matlab system block yields a sorted list of the numbers generated in the random number generator block. an example of this can be seen below.