- 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).
- 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.
How to convert matlab to python?
142 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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:
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
For more information, check out: https://www.mathworks.com/help/compiler_sdk/gs/create-a-python-application-with-matlab-code.html
Hope it helps.
2 个评论
surya venu
2024-6-20
编辑:surya venu
2024-6-20
Check out this link: https://www.mathworks.com/help/compiler_sdk/gs/create-a-python-application-with-matlab-code.html
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 Center 和 File Exchange 中查找有关 Python Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!