Printing to MATLAB command window using a Python Function

34 ビュー (過去 30 日間)
Tyler
Tyler 2019 年 12 月 12 日
コメント済み: Brian Harris 2022 年 3 月 22 日
I am wondering if anyone has been able to print to the MATLAB command window using a Python function. Right now, I have my program interfaced where MATLAB calls on a Python Function and this executes correctly. However, the Python function is kind of complicated, so it would help me greatly with debugging if I was able to view the print statements that exist within my python script. Is there a way to interface MATLAB and Python to where these print statements get sent to MATLAB and shown on MATLAB's command window?

回答 (1 件)

Nicolas B.
Nicolas B. 2019 年 12 月 13 日
I have never tried this but there is a MATLAB API for python (Calling MATLAB from Python). Than, there is a special way to get connected to the running session of MATLAB (Connect Python to Running MATLAB Session).
Finally, you have here an example how to call MATLAB functions from python: Call User Scripts and Functions from Python
I see one potential difficulty in your case because you are running Python from MATLAB, it is highly probable that MATLAB will be waiting to the end of your script before displaying anything or maybe, it will display nothing (I haven't tested it). So once you have tried it, you would be more than welcome to post here your experience. It could be beneficial for the community.
  1 件のコメント
Brian Harris
Brian Harris 2022 年 3 月 22 日
Confirmed your guess that "it is highly probable that MATLAB will be waiting to the end of your script before displaying anything". Created a "printme.py" file with:
import time
def printme(wait: int=5):
print('mefirst')
print('flushme', flush=True)
time.sleep(wait)
print('mesecond')
then ran:
py.printme.printme();
and got both prints at the same time after a 5 second pause...

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by