Error using Python with Matlab: matlab.eng​ine.Matlab​ExecutionE​rror: OOPS is not supported

조회 수: 8 (최근 30일)
I am trying to learn how to use Python and Matlab together. I wrote a script just to practice a simple lsqcurvefit of some simple generated data. I keep getting this error for line 20: matlab.engine.MatlabExecutionError: OOPS is not supported. I haven't been able to find an answer to this problem. What is wrong with using matlab.inline this way? I am using Python IDLE version 3.9.13 and Matlab version R2023a.
import matlab.engine
import numpy as np
# Start MATLAB engine
matlab = matlab.engine.start_matlab()
# Generate sample data
x = np.linspace(0, 10, 100)
y = 2 * np.sin(x) + np.random.normal(0, 0.5, size=len(x))
# Convert data to 1D arrays
x = np.ravel(x)
y = np.ravel(y)
# Convert data to MATLAB arrays
x_matlab = matlab.double(x)
y_matlab = matlab.double(y)
# Define the fitting function
fit_func = matlab.inline('a * sin(b * x)', 'x', 'a', 'b')
# Call lsqcurvefit in MATLAB
params = matlab.lsqcurvefit(fit_func, matlab.double([1, 1]), x_matlab, y_matlab)
# Extract the optimized parameters
a_opt, b_opt = params[0], params[1]
# Print the optimized parameters
print("Optimized parameters:")
print("a =", a_opt)
print("b =", b_opt)
# Stop MATLAB engine
matlab.quit()

답변 (1개)

Hitesh
Hitesh 2025년 1월 28일
편집: Hitesh 2025년 1월 28일
Hi Kelly,
The "inline" function is not recommended by MATLAB documentation. You need to use the "eval" function to accomplish the same functionality. I have updated your code, and it executed successfully. Please see the revised version below in the below image:
For more information related to "eval" fucntion, kindly refer to the following MATLAB documentation:

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by