Problems with input arguments in function

1 回表示 (過去 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()
eng.cd()
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';
end
and this is the error I'm getting:
  2 件のコメント
Andrey Smolyakov
Andrey Smolyakov 2020 年 6 月 17 日
It seems more like MATLAB just couldn't find your function. Make sure that it is located in the directory which is added to the MATLAB path. And, probably, read this.
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...

サインインしてコメントする。

回答 (1 件)

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: -

カテゴリ

Help Center および File ExchangeCall MATLAB from Python についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by