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.
0 件のコメント
回答 (1 件)
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.
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Call MATLAB from Python についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!