Error using Python with Matlab: matlab.engine.MatlabExecutionError: OOPS is not supported
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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()
Réponses (1)
Hitesh
le 28 Jan 2025
Modifié(e) : Hitesh
le 28 Jan 2025
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:
0 commentaires
Voir également
Catégories
En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!