Getting python terminal output
49 次查看(过去 30 天)
显示 更早的评论
I am calling a python function from Matlab that usually to take a couple of hours to finish a run. If the function is run as a proper Python script from Windows terminal, it prints out the operations being done etc. i.e. displays a log:
Cleaning Sig_Traces 1
Loaded Matrix_Weights
Iteration 1 in progress.
Checking for fit ...
So, at any point of time, I have a clue about the progress. Now, how to make this displayable in Matlab's own terminal (or wherever - I don't care) when calling the function from Matlab? I cannot be really staring at a "Busy" footer in Matlab for hours.
2 个评论
Delprat Sebastien
2024-3-2
You cannot easily get the print output.
However, if you use the logging module, you can use this in your python code to send repports to matlab. This snipet may have to be reworked but you should have the overall idea
import logging
HANDLER = logging.StreamHandler()
FORMATTER = logging.Formatter('[%(levelname)s] %(filename)s - %(lineno)d - %(message)s')
HANDLER.setFormatter(FORMATTER)
LOGGER = logging.getLogger('YOUR MODULE NAME') # <= modify here
LOGGER.addHandler(HANDLER)
LOGGER.setLevel(logging.WARNING) # pick whatever level suits
% Then in your code (pick one of these)
LOGGER.info('my message')
LOGGER.warning('my message')
LOGGER.error('my message')
回答(2 个)
Walter Roberson
2023-4-19
In the special case where you are running Python from Windows, then you could use the .NET assembly System.Diagnostics.Process to run the python executable while being able to access its standard output.
2 个评论
Walter Roberson
2023-4-19
Mathworks does not provide much information on the topic because Mathwork's only concern is being able to interface with .NET assemblies in general; what you can do with those assemblies is up to Microsoft and the people who write the assemblies.
The following posts may be useful
Al Danial
2023-4-20
The issue is that matlab buffers Python's print statements and only flushes this buffer--meaning print to the Python text in the matlab command window--when enough text has been added. All you need to do is print a bunch of useless text from Python to fill the buffer before printing your progress message. The best "useless" text is that which has no negative impact on the output you want to see. To that end I use a large number of copies of a space followed by a backspace, so a net zero of cursor movement, and in the end your text appears as it would normally.
Here's sample Python code that does this:
import time
def compute(n_iter):
for i in range(int(n_iter)):
time.sleep(0.4)
x = i*i / 7.3 - 0.2
print(' \b'*4000, end='') # <- this is the trick
print(f'{i:3d} {x: 10.6f}')
The second to last line fills the print buffer with no effect on the position of the "real" text which is to be printed (on the last line in this example). Tested on Linux with 2022b.
1 个评论
Walter Roberson
2023-4-20
However this is not relevant to the behaviour of .NET System.Diagnostics.Process.
.NET -- 4K buffer size, prone to deadlock
But .NET notifications can be delayed until newline is received
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Python Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!