With 2021b's New Python Functionality, is there a way to view an interpreter's output?

조회 수: 1 (최근 30일)
I have a python watch dog that monitors changes to a directory. When I run in VSCode I get messages from the script via print statements. Is there a way to have python print to the matlab command window?
I also use python 'return' statements in the python functions. How do you get the value returned from the event function back up to Matlab?
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
def on_created(event):
print(f"New Item {event.src_path}")
new_path = {event.src_path}
return new_path
def on_deleted(event):
print(f"Deleted Item {event.src_path}")
new_path = {event.src_path}
return new_path
def on_modified(event):
print(f"Modified Item {event.src_path}")
new_path = {event.src_path}
return new_path
if __name__ == "__main__":
patterns = ["*"]
ignore_patterns = None
ignore_directories = False
case_sensitive = True
my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
my_event_handler.on_created = on_created
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
my_event_handler.on_moved = on_moved
path = "SomeFolderPath"
go_recursively = True
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=go_recursively)
my_observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
my_observer.stop()
my_observer.join()
I'm not asking Mathworks to start supporting python, just wondering what is the best way for the two to communicate here.

답변 (1개)

Sean de Wolski
Sean de Wolski 2021년 10월 26일
편집: Sean de Wolski 2021년 10월 26일
>> pyrunfile('foo.py') % saving as foo.py, changing folder, and commenting broken on_moved
Modified Item C:\Documents\MATLAB\Scratch\test.txt
Modified Item C:\Documents\MATLAB\Scratch\test.txt
Seems to work for me, but it's blocking. If you want to get outputs from it, package it as a class, instantiate it, and then invoke its methods when you need an output.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by