フィルターのクリア

How to call a matlab plot function from python with arguments?

23 ビュー (過去 30 日間)
Nicholas Clavette
Nicholas Clavette 2020 年 7 月 13 日
編集済み: Kushal Malhotra 2021 年 7 月 27 日
Hi all,
I am trying to create 3D graphs in python and cannot use matplotlib, as I need to be able to plot with log scales. This led me to controlling matlab via python. I am trying to create a function to plot a 3d graph. When I call my function in python, I get an error saying "not enough input arguments". Here is my matlab code, python code, and screenshot of error. Can anyone point me in the right direction?
matlab, tplot.m
function tplot(X, Y, Z)
figure
plot3(X, Y, Z)
end
python
import matlab.engine
eng = matlab.engine.start_matlab()
X = matlab.double([1, 2, 3, 4, 5])
Y = matlab.double([5, 6, 7, 8, 5])
Z = matlab.double([9, 10, 11, 12, 13])
eng.tplot(X, Y, Z, nargout=0)
eng.close()
and lastly my error:

回答 (1 件)

Sai Sri Harsha Vallabhuni
Sai Sri Harsha Vallabhuni 2020 年 7 月 14 日
Instead of
eng.tplot(X, Y, Z, nargout=0)
Try this
eng.run("\path\to\tplot.m\file", X, Y, Z, nargout = 0)
Hope this helps.
  2 件のコメント
Nicholas Clavette
Nicholas Clavette 2020 年 7 月 14 日
Hi Sai,
Thanks for your response. That did not work unfortunately. I've graduated to using Matlab Library Compiler and Matlab runtime to create my plot. I get no errors, but the plot only displays for a millisecond. Any advice?
import matlab
import tPlotPkg
X = matlab.double([1, 2, 3, 4, 5])
Y = matlab.double([6, 7, 8, 9, 10])
Z = matlab.double([1, 2, 3, 4, 5])
myPlot = tPlotPkg.initialize()
myPlot.tplot(X, Y, Z, nargout=0)
Again, code compiles fine. I see a window flash on my screen (I am assuming this is my plot)? Then the program ends. Is there a way to stall while the window is open?
Kushal Malhotra
Kushal Malhotra 2021 年 7 月 27 日
編集済み: Kushal Malhotra 2021 年 7 月 27 日
Try a matlab function eng.hold("on",nargout=0). If that doesn't work then I would suggest it usually happens because the python script is terminated. So at the last line of your python program add time.sleep(1000). This won't terminated the current pthon program and allow you to view those figures for the amount of time mentioned in .sleep() method. The time is in seconds mentioned above. :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall MATLAB from Python についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by