Matlab engine for python- redirect output to diary
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello, I'm running Matlab function using matlab engine (through python) and i want to redirect the function output into a file. My code:
eng = matlab.engine.start_matlab("-r")
eng.cd(sys.argv[1])
funcName = sys.argv[2]
eng.eval("clc; diary out.txt; " + funcName + "; diary off;")
eng.quit()
The function is running and the file out.txt created however the output is not written to it. Can someone help me solve this problem. Thanks!
댓글 수: 2
Arthur
2017년 11월 24일
Same problem here.
Tried with this non-ascii diary workaround (from here) but this would unfortunately leave 'txt' var empty when called under python matlab engine.
% Output Command Window text to a text file
function saveCmdWinText(filename)
cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
txt = char(cmdWinDoc.getText(0,cmdWinDoc.getLength));
fid = fopen(filename,'W');
fwrite(fid,txt,'uint16'); % store as 2-byte characters
fclose(fid);
%winopen(filename); % in case you wish to verify...
end
답변 (1개)
Alan Frankel
2025년 7월 18일
Rather than an eval command:
>>> eng.eval("diary out.txt")
execute the MATLAB "diary" function directly from the engine handle:
>>> eng.diary("out.txt", nargout=0)
When I do this, I find that the output is captured in the diary file.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!