How to make a script return an output argument in python?

19 次查看(过去 30 天)
Hi I have linked python to matlab. I am running a script written in MATLAB in python. I am using as reference
I want my script to return the variable in my python window. How i can do it? Can i avoid this without converting it to a function?
b = 5;
h = 3;
a = 0.5*(b.* h)
import matlab.engine
eng = matlab.engine.start_matlab()
eng.triarea(nargout=0)
  1 个评论
yashvin
yashvin 2015-7-29
After you save the file, start Python and call the script.
import matlab.engine
eng = matlab.engine.start_matlab()
eng.triarea(nargout=0)
a =
7.5000
Specify nargout=0. Although the script prints output, it returns no output arguments to Python.
Convert the script to a function and call the function from the engine. Open the MATLAB editor to edit the file.
eng.edit('triarea',nargout=0)
I want for example return the variable a in my python window! How can i do that?

请先登录,再进行评论。

采纳的回答

Robert Snoeberger
Robert Snoeberger 2015-7-29
Scripts do not return output arguments, but they do store results in variables in the base workspace [1]. You can access the MATLAB engine workspace from Python [2].
Example
>>> import matlab.engine
>>> eng = matlab.engine.start_matlab()
>>> eng.triarea(nargout=0)
a =
7.5000
>>> a = eng.workspace['a'] # get the variable 'a' from the workspace
>>> a
7.5
>>>
References

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by