Problems with input arguments in function

조회 수: 2(최근 30일)
Jeniffer Viegas
Jeniffer Viegas 2020년 6월 16일
답변: Abhishek Gupta 2020년 9월 9일
Hi, I'm trying to call matlab from python and I'm having some problems, it seems the variable z is not the type matlab wants??
I only know that probably the problem is in this variable because when I modify the type to double or int the error is always with the same type.
this is my python script:
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
Nn = 30
x= 250*np.ones((1,Nn))
y= 100*np.ones((1,Nn))
zz = matlab.double([[32]])
xx = matlab.double(x.tolist())
yy = matlab.double(y.tolist())
Output = eng.simple_test(xx,yy,zz,nargout=4)
A = np.array(Output[0]).astype(float)
B = np.array(Output[1]).astype(float)
C = np.array(Output[2]).astype(float)
D = np.array(Output[3]).astype(float)
and this is my matlab function:
function [A,B,C,D] = simple_test(x,y,z)
A = 3*x+2*y;
B = x*ones(length(x),length(x));
C = ones(z);
D = x*y';
and this is the error I'm getting:
  댓글 수: 2
Jeniffer Viegas
Jeniffer Viegas 2020년 6월 17일
How do I know if it is in the Matlab path? I know that I put both scripts, the python and the matlab one in the same folder...

댓글을 달려면 로그인하십시오.


Abhishek Gupta
Abhishek Gupta 2020년 9월 9일
Hi Jeniffer,
Yes, as @Andrey suggested, you need to make sure that your folder containing the ‘simple_test’ function is on the MATLAB search path.
To check the same, you can follow the below steps: -
  1. Open MATLAB
  2. Go to Home tab -> Environment section -> Click Set Path
  3. In the Set Path dialog box, browse through the MATLAB search path, and see if your folder exists in the list. If not, then click 'Add with Subfolders…’ and select your folder containing your MATLAB files.
  4. Or you can also make use of the ‘addpath()’ function.
Also, referring you to the following links, which might help you in resolving the issue: -

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by