MatlabExecutionError when using Python API and calling user script
8 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Udit06
on 19 Feb 2024
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.
https://www.mathworks.com/help/matlab/matlab_external/use-the-matlab-engine-workspace-in-python.html
You can use a floating type value like 4.0 and the error should get resolved.
I hope this helps.
0 Comments
See Also
Categories
Find more on Call MATLAB from Python in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!