Log Error and Warning Messages to Text File

조회 수: 44 (최근 30일)
Sonoma Rich
Sonoma Rich 2022년 6월 16일
댓글: Jan 2022년 6월 17일
Is there a way to log error and warning messages to a text file? I cannot use the "diary" function since I am running Matlab within Cameo System Modeler and don't have a command window.
  댓글 수: 3
Sonoma Rich
Sonoma Rich 2022년 6월 17일
The reason has to do with the fact I am using Matlab within No Magic's Cameo System Modeler (CSM) application. It calls Matlab functions from within its simulation toolbox. Output to the command window seem to be disregarded. I tried using the "diary" function by typing "diary logfile.txt" from with the Matlab command line input in CSM, but it did not work. Also the "disp" function does not output messages.
Can I define the files for the output from stdout and stderr?
Walter Roberson
Walter Roberson 2022년 6월 17일
I would suggest using an absolute path for the diary file name, as you might not be cd to where you expect.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2022년 6월 17일
You can catch errors using try-catch and write the messages whereever you want.
Redirecting warnings is more tricky. I use my own warning functions, but warnings created by toolbox functions are harder to catch. You can shadow the built-in function "warning" by a user-defined function, which is store in a folder on top of Matlab's path:
function warning(varargin) % [UNTESTED CODE!!!]
Msg = evalc('builtin(''warning'', varargin{:})');
... % ==> write the message to your log file
end
This looks ugly, but maybe it works.
I do not trust code, which shows warnings. After inverting a matrix, I check the lastwarn state and drop an error in case of problems. If this happens repeatedly, I add some code to check condition of the matrix before the inverting. So I do not used code for productive work, which drops warnings.

Sonoma Rich
Sonoma Rich 2022년 6월 17일
I tried that. The diary file was created, but had nothing in it. I appears the CSM is overriding the stdout and stderr.
  댓글 수: 1
Jan
Jan 2022년 6월 17일
Is this an answer or a comment? In the latter case, please post it in the coirresponding section for comments. Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by