Python calls MATLAB built-in function griddedInterpolant(), how do I get the value of the query point?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
In fact, I need to use the 4D spline interpolation function in the experiment, but python has no API to call.The following is a simple one-dimensional example, I want to know how to get the value of the query point xq. Thanks a lot!
#---------------------------------------python code----------------------------------------#
import numpy as np
import matlab.engine
eng = matlab.engine.start_matlab()
x = np.array([1, 2, 3, 4, 5])
v = np.array([1.2, 0.6, 0.9, 4.5, 7.9])
x_new = matlab.double(x.tolist())
v_new = matlab.double(v.tolist())
F = eng.griddedInterpolant(x_new, v_new)
xq = np.array([2.5, 2.7, 3.6])
xq = matlab.double(xq.tolist())
vq = F(xq)
print(vq)
eng.quit() # quit Matlab engine
#--------------------------------------error message --------------------------------#
Traceback (most recent call last):
File "D:/Data/GitHub/CAE_CSI_mutillayer/test.py", line 84, in <module>
vq = F(xq)
TypeError: 'matlab.object' object is not callable
0 comentarios
Respuestas (1)
Madheswaran
el 18 de Feb. de 2025
Hi @xaofeng he
The problem you are facing is because MATLAB objects (like griddedInterpolant) cannot be called directly in Python. To solve this, you can use the following approach:
import numpy as np
import matlab.engine
eng = matlab.engine.start_matlab()
x = np.array([1, 2, 3, 4, 5])
v = np.array([1.2, 0.6, 0.9, 4.5, 7.9])
x_new = matlab.double(x.tolist())
v_new = matlab.double(v.tolist())
xq = np.array([2.5, 2.7, 3.6])
xq_new = matlab.double(xq.tolist())
eng.workspace['x'] = x_new
eng.workspace['v'] = v_new
eng.workspace['xq'] = xq_new
eng.eval('F = griddedInterpolant(x, v);', nargout=0)
vq = eng.eval('F(xq)', nargout=1)
print(vq)
eng.quit()
This code creates the interpolant and evaluates it by placing variables in the MATLAB workspace and using MATLAB's eval function to execute the commands.
You could also create a dedicated MATLAB function if you would like to avoid the use of 'eval' function. Create a file named 'interpolate_data.m' in your working directory,
% interpolate_data.m
function vq = interpolate_data(x, v, xq)
% Create interpolant
F = griddedInterpolant(x, v);
% Evaluate at query points
vq = F(xq);
end
and in python,
import numpy as np
import matlab.engine
eng = matlab.engine.start_matlab()
x = np.array([1, 2, 3, 4, 5])
v = np.array([1.2, 0.6, 0.9, 4.5, 7.9])
x_new = matlab.double(x.tolist())
v_new = matlab.double(v.tolist())
xq_new = matlab.double(np.array([2.5, 2.7, 3.6]).tolist())
vq = eng.interpolate_data(x_new, v_new, xq_new)
print(vq)
eng.quit()
This approach creates a dedicated MATLAB function that handles the interpolation, making the cleaer code.
For more information on using MATLAB with Python, refer to the following documentation: https://www.mathworks.com/help/matlab/matlab-engine-for-python.html
I hope this helps!
0 comentarios
Ver también
Categorías
Más información sobre Call MATLAB from Python en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!