How to convert matlab to python?

142 Ansichten (letzte 30 Tage)
Zhao TT
Zhao TT am 24 Mai 2024
Bearbeitet: Walter Roberson am 20 Jun. 2024
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.

Antworten (1)

surya venu
surya venu am 24 Mai 2024
Bearbeitet: surya venu am 20 Jun. 2024
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 Kommentare
Zhao TT
Zhao TT am 24 Mai 2024
Thanks for your answer. My module name is "testApp" as well, but it doesn't work.
surya venu
surya venu am 20 Jun. 2024
Bearbeitet: surya venu am 20 Jun. 2024
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Python Package Integration finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by