Как вызвать Matlab из Python, где входными данными является список ⇐ Python

Программы на Python
Anonymous
Как вызвать Matlab из Python, где входными данными является список

Сообщение Anonymous »

Я хочу вызвать функцию MATLAB в Python, но она продолжает говорить «неправильный тип».
Мой код Python выглядит следующим образом:

Код: Выделить всё

# Start MATLAB engine
eng = matlab.engine.start_matlab()

# Add the directory containing your MATLAB function to the MATLAB engine's path
eng.addpath(r"test_function_2D_matlab")

# Define the matrix in Python syntax
X_test = [
[4.1656, 2.1549],
[-1.5055, 6.4490],
[4.9775, 9.5070],
[9.0715, 13.6937],
[0.2693, 12.0323],
[7.1969, 4.0961],
[2.2232, 7.7268],
[-3.2281, 0.0208]
]

# Convert the Python list to a MATLAB compatible double matrix
X_matlab = matlab.double(X_test)

# Call the MATLAB function and store the result
Y = eng.test_function_2D_matlab(X_matlab)

# Print the result
print(Y)

# Close the MATLAB engine
eng.quit()
И код Matlab приведен ниже:

Код: Выделить всё

function Y = test_function_2D_matlab(X)
% Computes the Branin-Hoo test function

% Constants
pi_val = pi;
a = 1;
b = 5.1 / (2*pi_val)^2;
c = 5 / pi_val;
r = 6;
s = 10;
t = 1 / (8*pi_val);

% Make sure X is in the correct format
if size(X, 2) == 1
X = X'; % Transpose X if it is a column vector
end

% Compute the Branin-Hoo function
Y = a * (X(:,2) - b * (X(:,1).^2) + c * X(:,1) - r).^2 + s * (1 - t) * cos(X(:,1)) + s;
end
И когда я запущу его, у меня будет такая ошибка:

Код: Выделить всё

{
"name": "MatlabExecutionError",
"message": "Undefined function 'test_function_2D_matlab' for input arguments of type 'double'.
",
"stack": "---------------------------------------------------------------------------
MatlabExecutionError                      Traceback (most recent call last)
Cell In[35], line 19
16 X_matlab = matlab.double(X_test)
18 # Call the MATLAB function and store the result
---> 19 Y = eng.test_function_2D_matlab(X_matlab)
21 # Print the result
22 print(Y)

anaconda3\\envs\\uqlab\\lib\\site-packages\\matlab\\engine\\matlabengine.py:71, in MatlabFunc.__call__(self, *args, **kwargs)
68     return FutureResult(self._engine(), future, nargs, _stdout, _stderr, feval=True)
69 else:
70     return FutureResult(self._engine(), future, nargs, _stdout,
---> 71                         _stderr, feval=True).result()

\\anaconda3\\envs\\uqlab\\lib\\site-packages\\matlab\\engine\\futureresult.py:67, in FutureResult.result(self, timeout)
64     if timeout < 0:
65         raise TypeError(pythonengine.getMessage('TimeoutCannotBeNegative'))
---> 67 return self.__future.result(timeout)

anaconda3\\envs\\uqlab\\lib\\site-packages\\matlab\\engine\\fevalfuture.py:82, in FevalFuture.result(self, timeout)
79 if not result_ready:
80     raise TimeoutError(pythonengine.getMessage('MatlabFunctionTimeout'))
---> 82 self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
83 self._retrieved = True
84 return self._result

MatlabExecutionError: Undefined function 'test_function_2D_matlab' for input arguments of type 'double'.
"
}
С нетерпением жду ваших решений.
Я пытаюсь преобразовать все элементы один за другим в matlab.doule, но это не сработало.< /п>

Подробнее здесь: https://stackoverflow.com/questions/790 ... ut-is-list

Вернуться в «Python»