Python script changes are not updated in MATLAB

조회 수: 17 (최근 30일)
Ali Tarik Karagöz
Ali Tarik Karagöz 2024년 4월 25일
답변: Nando 2024년 11월 14일 12:54
I am trying to use MATLAB with some Python integration. However, when I want to apply some changes to the python scripts/functions I need to restart MATLAB to use the updated version.
I have seen some threads/replies from the past but I was not sure if there is a new way to reload the scripts or clear old information.
(matlab script)
clc
clear
clear workspace
pyenv(Version = "C:\Users\m85830ak\Python\Matlab-python\.venv\Scripts\python.exe");
fun = @trial;
x0 = [150,120e5,7850000.0]
output = fun(x0)
A=[];
b=[];
Aeq=[];
beq=[];
lb = [150,74e5,74e5];
ub = [530,300e5,300e5];
[x,fval]= fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
trial.m (matlab function file)
function a = trial(x)
a=py.matlab_file.turbine(x(1),x(2),x(3));
a = -a;
matlab_file.py (Python script)
from pyfluids import Fluid, FluidsList, Input
def turbine(tin, pin, pout):
ntur = 85
m=100
turb_out = (
Fluid(FluidsList.CarbonDioxide)
.with_state(Input.temperature(tin), Input.pressure(pin))
.expansion_to_pressure(pout, ntur)
)
turb_inlet = Fluid(FluidsList.CarbonDioxide).with_state(
Input.temperature(tin), Input.pressure(pin)
)
delta_h = turb_inlet.enthalpy - turb_out.enthalpy
w_tur = delta_h * m
return w_tur
  댓글 수: 1
Ali Tarik Karagöz
Ali Tarik Karagöz 2024년 4월 26일
clear classes at the beginning of the script and
function a = trial(x)
mf = py.importlib.import_module("matlab_file");
py.importlib.reload(mf);
% y =[9, 4, 7, 5, 7, 4, 1, 5, 2, 3, 4];
a=mf.SQPOPT(x);
this reload is working.

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

답변 (2개)

Hassaan
Hassaan 2024년 4월 25일
py.reload(py.matlab_file);
Try using py.reload()
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  댓글 수: 1
Ali Tarik Karagöz
Ali Tarik Karagöz 2024년 4월 26일
편집: Ali Tarik Karagöz 2024년 4월 26일
No module or function named 'reload'.
Error in untitled (line 6)
py.reload();
I tried py.reload(matlab_file) as well. Both gave this error

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


Nando
Nando 2024년 11월 14일 12:54
This question has been answered here
The solution is
warning('off','MATLAB:ClassInstanceExists')
clear classes
m = py.importlib.import_module('PythonFilename');
py.importlib.reload(m);

카테고리

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

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by