필터 지우기
필터 지우기

YAQ about reloading python modules in Matlab after python changes

조회 수: 10 (최근 30일)
Alexander
Alexander 2023년 7월 27일
댓글: Alexander 2024년 1월 1일
I understand that there have been a number of questions regarding issues with Matlab not reloading python modules after they have been changed. Based off other answers, I have an acceptable solution, but I think I am hitting a bug.
My python:
class myTestClass:
def __init__(self, somestr):
self.loc = 1234
print("Init called")
def __del__(self):
print("Destructor")
def myTestFunc(self):
a = 456
print(f'My Test Func called loc={self.loc} a={a}')
My Matlab:
%clear classes
myMatlabTestClass = py.importlib.import_module('myMatlabTestClass');
py.importlib.reload(myMatlabTestClass);
pyclass = myMatlabTestClass.myTestClass("hello");
pyclass.myTestFunc()
The problem:
The above is sensitive to python code changes if I change the value of self.loc, but changing the value of a in myTestFunc requires the inclusion of clear classes.
I fear that clear classes will be too expensive and may impact other code, depending on where it is called.
I'd like to understand why a and self.loc would be treated differently here and if there is a workaround.
  댓글 수: 1
Alexander
Alexander 2023년 7월 27일
Not quite what I wanted, but this code works flawlessly:
myMatlabTestClass = py.importlib.import_module('myMatlabTestClass');
py.importlib.reload(myMatlabTestClass);
pyrun("import myMatlabTestClass")
pyrun("myClass = myMatlabTestClass.myTestClass('somestr')")
pyrun("myClass.myTestFunc()")

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

답변 (1개)

SHAQLAIN
SHAQLAIN 2023년 12월 24일
class myTestClass:
def __init__(self, somestr):
self.loc = 1234
print("Init called")
def __del__(self):
print("Destructor")
def myTestFunc(self):
a = 456
print(f'My Test Func called loc={self.loc} a={a}')
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 12월 24일
Could you point out the difference between this and what was originally posted?
Alexander
Alexander 2024년 1월 1일
The problem is not the python code, it is in matlab reloading the python code when changes are made to it.

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

카테고리

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

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by