MATLAB Answers

call matlab function from python but the argument is always mismatch

7 views (last 30 days)
dh tang
dh tang on 20 Dec 2019
Commented: dh tang on 11 May 2020
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 Comment

Matt J
Matt J on 20 Dec 2019
Note: For years, fanbeam has exhibited weird non-linear behavior, which nobody seems to understand. I wouldn't trust it.

Sign in to comment.

Accepted Answer

Shrinidhi KR
Shrinidhi KR on 8 May 2020
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!