필터 지우기
필터 지우기

Matlab Compiler SDK - Export Class to Python

조회 수: 11 (최근 30일)
Pier-Yves Lessard
Pier-Yves Lessard 2021년 12월 15일
답변: Akash 2024년 3월 4일
Hi,
I've tried Matlab Compiler SDK to export code in Python and it seems to work pretty well.
I'd like to export some object oriented code but can't figure out how to use the generated Python module.
Say I export a class called "ClassA". Then I can initialize the generated module, and instantiante "ClassA" which return a "matlab.object".
After that, I don't know how to call the object methods. The functions does not exist in the object nor in the module. How can I do?
Matlab
classdef MatlabClassA
properties
c
end
methods
function obj = MatlabClassA(a,b)
obj.c = a + b;
end
function x = get_val(self)
x = self.c;
end
end
end
Python
import MatlabClassAModule
handle = MatlabClassAModule.initialize()
a = handle.MatlabClassA(10,20) # Creates an object
x = a.get_val() # does not exit. Ho do I do?
Thank you
  댓글 수: 1
Christopher Wunder
Christopher Wunder 2022년 9월 13일
This might be late, but you cannot access matlab objects from python as far as I know.
The only workaround I know is, you pass this object to a matlab function and access its methods and attributes there. If you alter the object you can return it anew and "use" it in your python code. So in your example you would write an extra matlab function:
function value = get_val_from_object(myobject)
value = myobject.get_val()
end
and then access it like so:
handle = MatlabClassAModule.initialize()
a = handle.MatlabClassA(10,20)
x = handle.get_val_from_object(a)

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

답변 (1개)

Akash
Akash 2024년 3월 4일
Hi Pier,
You can utilize MATLAB "Handle" class to create an object and call its methods in Python. For a detailed guide on how to make your MATLAB class a "Handle" class and utilize it within Python, you can refer to the discussion link provided below:-
For more information on how to work with MATLAB "Handle" objects in Python, you can utilize the documentation provided below:-

카테고리

Help CenterFile Exchange에서 Python Package Integration에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by