Matlab engine for python- redirect output to diary
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Arthur
il 24 Nov 2017
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
Risposte (1)
Alan Frankel
il 18 Lug 2025
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 Commenti
Vedere anche
Categorie
Scopri di più su Call Python from MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!