Filter löschen
Filter löschen

MatlabExecutionError when using Python API and calling user script

7 Ansichten (letzte 30 Tage)
Marc van den Berg
Marc van den Berg am 11 Okt. 2021
Beantwortet: Udit06 am 19 Feb. 2024
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.

Antworten (1)

Udit06
Udit06 am 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.
You can use a floating type value like 4.0 and the error should get resolved.
I hope this helps.

Kategorien

Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by