MatlabExecutionError when using Python API and calling user script

8 次查看(过去 30 天)
Hi there,
I am new to using the python API for matlab and am getting an error which I cannot understand and am following the example found here Call User Scripts and Functions from Python.
My matlab script "simple_script.m" is a simple:
a = 'Test'
In python, in the correct working directory containing "simple_script.m", I try to run:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.Simple_script(nargout=0)
However, this returns nothing. That is, if I run
a = eng.Simple_script(nargout=0)
it returns a NoneType object in python. If I create a function out of the script, it does work, but I would be interested to run scripts as well.
The engine seems to work for some cases, although the example Use MATLAB Engine Workspace in Python also gives errors (raises: "Undefined function 'sqrt' for input arguments of type 'int64'."; but it is not what I am interested in anyways).
I am running
  • Python 3.8.8 in spyder (anaconda)
  • Matlab 2021a
Let me know if you need any extra information.
And thank you in advance for your help.

回答(1 个)

Udit06
Udit06 2024-2-19
Hi Marc,
Since your simple_script.m contains only an assignment statement, it is not returning anything to the python. In my opinion, this is the expected behaviour, since assignment operator does not return anything.
When you create a function out of the script, it will return a value if the function that you have created returns a value.
The error Undefined function 'sqrt' for input arguments of type 'int64' that you are facing is probably because you might have initialized the value of x to 4 instead of 4.0 in the code given in the following MathWorks documentation.
You can use a floating type value like 4.0 and the error should get resolved.
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by