call matlab function from python but the argument is always mismatch

조회 수: 7 (최근 30일)
dh tang
dh tang 2019년 12월 20일
댓글: dh tang 2020년 5월 11일
I want to call fanbeam, the function defines in matlab like
proj = fanbeam(image, 1075,...
'FanSensorGeometry', 'arc',...
'FanSensorSpacing',0.1, ...
'FanRotationIncrement', 360/320);
At first I call the fanbeam in python like
proj = eng.fanbeam(image,1075.0,nargout=3)
it works well,But I want add some argument 'FanSensorGeometry', 'FanSensorSpacing',so I try to wrap them as a dict then pass the dict to fanbeam
para_dic = {"FanSensorSpacing":0.1,"FanRotationIncrement" :360.0/320}
proj = eng.fanbeam(hello,1075.0,para_dic,nargout=3)
But I got a wrong message
File "/media/Data/MyModel/train.py", line 118, in <module>
proj = eng.fanbeam(hello,1075.0,para_dic)
File "/home/wd/anaconda3/lib/python3.7/site-packages/matlab/engine/matlabengine.py", line 71, in __call__
_stderr, feval=True).result()
File "/home/wd/anaconda3/lib/python3.7/site-packages/matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/home/wd/anaconda3/lib/python3.7/site-packages/matlab/engine/fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.MatlabExecutionError:
File /usr/local/MATLAB/R2019b/toolbox/images/images/private/check_fan_params.m, line 20, in check_fan_params
File /usr/local/MATLAB/R2019b/toolbox/images/images/fanbeam.m, line 168, in fanbeam
Function FANBEAM expected an even number of parameter/value arguments.
So,What should I do to pass these arguments to fanbeam function from python?
  댓글 수: 1
Matt J
Matt J 2019년 12월 20일
Note: For years, fanbeam has exhibited weird non-linear behavior, which nobody seems to understand. I wouldn't trust it.

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

채택된 답변

Shrinidhi KR
Shrinidhi KR 2020년 5월 8일
You can just call it and specify the parameters directly, it worked for me.
import matlab.engine
eng = matlab.engine.start_matlab()
I = eng.eval('ones(100)')
D = eng.eval('200')
dtheta = eng.eval('45')
proj = eng.fanbeam(I, D,'FanSensorGeometry', 'arc','FanSensorSpacing',0.1,'FanRotationIncrement',dtheta)
I have used eval to avoid any datatype mismatch from matlab into python. Alternatively you can try to call fanbeam function inside eval function as well, but have to handle the string quotations properly

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by