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

5 vues (au cours des 30 derniers jours)
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)
while True:
except KeyboardInterrupt:
I'm not asking Mathworks to start supporting python, just wondering what is the best way for the two to communicate here.

Réponses (1)

Sean de Wolski
Sean de Wolski le 26 Oct 2021
Modifié(e) : Sean de Wolski le 26 Oct 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.


En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by