Effacer les filtres
Effacer les filtres

Calling Python modules in MATLAB function Simulink Block

4 vues (au cours des 30 derniers jours)
Shray
Shray le 17 Juin 2023
I have created a m-script file by calling Pyhton Modules the code is given below:-
tdb = 25;
tr = 25;
rh = 50;
v = 0.1;
met = 1.4;
clo = 0.5;
v_r=py.pythermalcomfort.utilities.v_relative(v=v, met=met);
clo_d=py.pythermalcomfort.utilities.clo_dynamic(clo=clo, met=met);
pmv=py.pythermalcomfort.pmv_ppd(tdb=tdb, tr=tr, vr=v_r, rh=rh, met=met, clo=clo_d);
display(pmv)
The output of the code is perfect as expected which is as follows:-
pmv =
Python dict with no properties.
{'pmv': 0.06, 'ppd': 5.1}
Now i am trying to create a Simulink Block using this m-script Code. I am using MATLAB function block for this and put the following code in the MATLAB functon code generator block:-
function PMV = PMV_BLOCK(tdb, tr, vr, rh, met, clo)
tdb = 25;
tr = 25;
rh = 50;
v = 0.1;
met = 1.4;
clo = 0.5;
coder.extrinsic('py.pythermalcomfort.utilities.v_relative', 'py.pythermalcomfort.utilities.clo_dynamic','py.pythermalcomfort.pmv_ppd')
v_r=py.pythermalcomfort.utilities.v_relative(v=v, met=met);
clo_d=py.pythermalcomfort.utilities.clo_dynamic(clo=clo, met=met);
pmvc=py.pythermalcomfort.pmv_ppd(tdb=tdb, tr=tr, vr=v_r, rh=rh, met=met, clo=clo_d)
PMV=double(0);
PMV = pmvc;
when i run this program following error is displayed:-
Python Error: TypeError: v_relative() takes 2 positional arguments but 4 were given Error in 'PMV_Block/PMV Calculator' (line 9) v_r=py.pythermalcomfort.utilities.v_relative(v=v, met=met);
I don't know why this python error is coming although the code is running fine in MATLAB m-script file. I am using a third party Python module. ANy urgent help in this regard will be greatly appreciated.

Réponses (1)

Kautuk Raj
Kautuk Raj le 18 Juin 2023
The issue you are encountering is due to the way MATLAB handles Python function calls in the Simulink environment. In MATLAB scripts, you can use keyword arguments when calling Python functions, but this is not supported in the MATLAB Function block in Simulink.
To resolve this problem, you can create a wrapper MATLAB function for the Python function calls using positional arguments instead of keyword arguments. Create a new file called pythermalcomfort_wrapper.m with the following content:
function [v_r, clo_d, pmv_ppd] = pythermalcomfort_wrapper(v, met, clo, tdb, tr, rh)
coder.extrinsic('py.pythermalcomfort.utilities.v_relative', ...
'py.pythermalcomfort.utilities.clo_dynamic', ...
'py.pythermalcomfort.pmv_ppd');
v_r = py.pythermalcomfort.utilities.v_relative(v, met);
clo_d = py.pythermalcomfort.utilities.clo_dynamic(clo, met);
pmv_ppd = py.pythermalcomfort.pmv_ppd(tdb, tr, rh, v_r, met, clo_d);
end
Now, in your MATLAB Function block in Simulink, call this wrapper function instead of the Python functions directly:
function PMV = PMV_BLOCK(tdb, tr, vr, rh, met, clo)
tdb = 25;
tr = 25;
rh = 50;
v = 0.1;
met = 1.4;
clo = 0.5;
[v_r, clo_d, pmv_ppd] = pythermalcomfort_wrapper(v, met, clo, tdb, tr, rh);
PMV = double(pmv_ppd{'pmv'});
end
This should resolve the issue with calling Python functions in the MATLAB Function block, allowing you to use the pythermalcomfort Python package within your Simulink model.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by