How to convert matlab to python?

142 次查看(过去 30 天)
Zhao TT
Zhao TT 2024-5-24
I use library compiler to convert matlab code to python, my code listed. The conversion is successful but I can't use the code in python.
Matlab Code:
classdef testApp
properties
a1 = 0;
a2 = 0;
end
methods
function obj = start(obj,x1,x2)
obj.a1 = x1;
obj.a2 = x2;
end
end
end
Python Code:
import matlab
import testApp
aaa = testApp.initialize()
a = aaa.start(1,2)
vv = 1
Error:
SystemError: Error in MATLAB Compiler SDK for Python. Details: An error occurred during evaluation of the function start. Details: Function start not found in Python package.

回答(1 个)

surya venu
surya venu 2024-5-24
编辑:surya venu 2024-6-20
Hi,
The issue lies in how you're trying to use the converted Python code. Here's the problem and the fix:
Problem:
The Library Compiler creates a Python package, not a directly usable class. You're trying to call the "start" method on the "testApp" class directly, which isn't the intended approach.
Fix:
  1. Import the module:Instead of importing "testApp", import the module generated by the Library Compiler. It likely has a different name (check the generated files).
  2. Use the initialize function:The Library Compiler typically provides an "initialize" function to create an instance of the converted class. Use this function to create an object and then call its methods.
Here's the corrected Python code:
import <module_name> # Replace with the actual module name
# Create an instance of the class
aaa = <module_name>.initialize()
# Call the start method on the instance
a = aaa.start(1, 2)
vv = 1
Hope it helps.
  2 个评论
Zhao TT
Zhao TT 2024-5-24
Thanks for your answer. My module name is "testApp" as well, but it doesn't work.
surya venu
surya venu 2024-6-20
编辑:surya venu 2024-6-20
In the Install and Run MATLAB Generated Python Application section if you see,
import <Module_Name>
# Import the matlab module only after you have imported
# MATLAB Compiler SDK generated Python modules.
import matlab
In your case it's done vise versa.
Let me know, after changing if it works.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Python Package Integration 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by