Redirecting error and warning messages to a file when running a deployed MATLAB GUI
12 次查看(过去 30 天)
显示 更早的评论
I am developing a MATLAB GUI that is transitioning from alpha to beta. I would like to be able to receive debug logs from the users. I use disp, error and warning at different places in the code. Is it possible to deploy the GUI such that the messages destined to MATLAB's command window are redirected to stdout or file (say debug.txt)?
Thanks,
Shalin
0 个评论
回答(1 个)
Image Analyst
2014-1-12
See if the "diary" command is what you're looking for.
You can also write just errors to a log file if you catch them and call a function, like WarnUser() in the code below, that will alert the user and append the message to a log file.
try
% Your code goes here.
catch ME
% You get here if there was an error in this function
% or some function this function called that was unhandled.
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
WarnUser(errorMessage);
end
====================================================================
function WarnUser(errorMessage)
% Alert user via the command window and a popup message.
fprint(1, '%s\n', errorMessage); % To command window.
uiwait(warndlg(errorMessage));
% Open the Error Log file for appending.
fullFileName = 'Error Log.txt';
fid = fopen(fullFileName, 'at');
fprint(fid, '%s\n', errorMessage); % To file
fclose(fid);
return; % from WarnUser()
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!