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

2 visualizaciones (últimos 30 días)
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.

Respuestas (1)

Sean de Wolski
Sean de Wolski el 26 de Oct. de 2021
Editada: Sean de Wolski el 26 de Oct. de 2021
>> 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.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by