Running a MATLAB script in Python: "Operator '*' is not supported for operands of type 'cell'."
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to run a MATLAB script in Python to generate a trajectory for the Lorenz system. My Python code is:
import matlab.engine
eng = matlab.engine.start_matlab()
traj = eng.gen_lorenz([1, 1, 1],10,8/3,28,250,1/50)
eng.quit()
print(traj)
My MATLAB function is:
function [output] = gen_lorenz(init_pos,sigma,beta,rho,tf,dt)
%GEN_LORENZ
%Generates Lorenz Trajectory based on initial position (init_pos),
%sig/rho/beta values, every dt seconds to a final time (tf)
f = @(t,a) [-sigma*a(1) + sigma*a(2);
rho*a(1) - a(2) - a(1)*a(3);
-beta*a(3) + a(1)*a(2)];
[~,a] = ode45(f,[double(0):dt:double(tf)], init_pos);
output = a;
end
When I run the code, I keep getting the error "Operator '*' is not supported for operands of type 'cell'" on the line: f = @(t,a) ...
I'm assuming it's a problem with trajectory array (a) or the sig/beta/rho constants, but I'm not sure why they're being interpreted as a cells. The function works fine if it's run just on MATLAB.
Thanks
댓글 수: 4
Hanjin Liu
2021년 2월 21일
The following code worked well in my PC with a copy of your MATLAB function.
from matlab import double
traj = eng.gen_lorenz(double([1, 1, 1]),10.0,8/3,28.0,250.0,1/50)
You got a ValueError because matlab.double only takes arguments that are iterable.
matlab.double(10) ... error.
matlab.double([10]) ... works, but 10.0 is simpler because float in Python will be automatically converted to MATLAB double.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!