Error in running the python Code in MATLAB function Block

5 views (last 30 days)
I have prepared a code for MATLAB function block given below:-
function pmvf = PMV_BLOCK(tdb, tr, vr, rh, met, clo)
pmvf=0;
ppdf=0;
tdb=40;
tr = 30;
rh = 50;
vr = 0.1;
met = 1.3;
clo = 0.8;
coder.extrinsic('py.pythermalcomfort.utilities.v_relative');
v_r=double(py.pythermalcomfort.utilities.v_relative(vr, met)); %to calculate relative velocity from absolute velocity
coder.extrinsic('py.pythermalcomfort.utilities.clo_dynamic');
clo_d=double(py.pythermalcomfort.utilities.clo_dynamic(clo, met)); %to calculate clothing insulation
coder.extrinsic('py.pythermalcomfort.pmv_ppd', 'struct', 'struct2cell');
pmvx= struct('pmv', [], 'ppd', []);
pmvx=py.pythermalcomfort.pmv_ppd(tdb, tr, v_r, rh, met, clo_d); %to calculate pmv and ppd
coder.extrinsic('py.list');
coder.extrinsic('keys', 'values', 'cell', 'cellfun');
k=py.list(keys(pmvx));
x=py.list(values(pmvx));
y=double(x);
pmvf=y(1);
This code is running just fine with the output that i intend to produce. However, when i change the line of pmvx=py.pythermalcomfort.pmv_ppd(tdb, tr, v_r, rh, met, clo_d) to pmvx=py.pythermalcomfort.pmv_ppd(tdb, tr, v_r, rh, met, clo_d, standard="ashrae"). The MATLAB function block is giving the following error:-
Python Error: TypeError: ufunc 'two_nodes_optimized_return_set' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' Error in two_nodes Error in set_tmp Error in cooling_effect Error in _get_ufunc_and_otypes Error in _vectorize_call Error in __call__ Error in pmv_ppd Error in 'PMV_Block/PMV Calculator' (line 15)
Component:Simulink | Category:Model error
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • Simulation stopped because of a runtime error.
When i run this code in MATLAB editor the program is running just fine with the following output.
>> PMV
Python list with values:
[2.95, 98.9]
Use string, double or cell function to convert to a MATLAB array.
{[2.9500]} {[98.9000]}
2.9500 98.9000
ans =
2.9500
Any urgent help in this regard would be greatly appreciated.

Answers (1)

Shishir Reddy
Shishir Reddy on 24 Jul 2025
You are encountering this issue because the MATLAB Function block in Simulink has stricter type and code generation constraints, especially when using coder.extrinsic to call Python functions. When you add the standard="ashrae" keyword argument, MATLAB internally creates a Python str object using "ashrae", but the Simulink environment may not handle keyword arguments or certain Python object types correctly inside code generation workflows.
To resolve this, use a Python dictionary for keyword arguments:
Since 'py.pythermalcomfort.pmv_ppd()' accepts keyword arguments, you can try constructing a dict explicitly:
matlabCopyEditcoder.extrinsic('py.dict');
kwargs = py.dict(pyargs('standard', 'ashrae'));
pmvx = py.pythermalcomfort.pmv_ppd(tdb, tr, v_r, rh, met, clo_d, kwargs);
Another approach would be to move the Python-related code into a separate MATLAB script or function outside the Simulink block, and pass the computed PMV value into Simulink as a variable or using an S-Function or From Workspace block. This avoids Simulink's codegen limitations.
I hope this helps.

Community Treasure Hunt

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

Start Hunting!