Error using Python with Matlab: matlab.engine.MatlabExecutionError: OOPS is not supported
Mostrar comentarios más antiguos
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 comentario
Kautuk Raj
el 13 de Jun. de 2023
Respuestas (1)
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:
Categorías
Más información sobre Call MATLAB from Python en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!