Using try/catch to get warning message
43 次查看(过去 30 天)
显示 更早的评论
What o I have to do in order to get rid of the message in ode45 line 309 (error tolerance message). I use try and then I put the ode45(......) and eventually I`ll have a warning message about the tolerance for integration on the screen but I do not want this warning poping up all the time so I save the line 309 in ode45 and put it in a catch statement but it didnt work. Can you guys help me with that.
2 个评论
Geoff Hayes
2014-7-22
What is the full warning message? What is your line of code?
The try/catch will only be useful to capture thrown errors and will not suppress a warning message.
采纳的回答
Kelly Kearney
2014-7-22
You can turn off specific warning messages via the warning command:
S = warning('MATLAB:ode45:IntegrationTolNotMet', 'off');
... add your ode call here ...
warning(S);
2 个评论
Kelly Kearney
2014-7-22
Oops, as you can see from IA's answer below, I reversed the inputs (ugh, I always do that with this command in my code too).
Should be
S = warning('off', 'MATLAB:ode45:IntegrationTolNotMet');
更多回答(1 个)
Image Analyst
2014-7-22
Here's a useful function I run at the beginning of my larger m-files. It gives instructions for how to find out the error message and how to turn it off. It will be easy to adapt it to your message. The actual file is attached below this:
% http://www.mathworks.com/help/matlab/matlab_prog/suppress-warnings.html
function TurnOffWarnings
try
% To set the warning state, you must first know the message identifier for the one warning you want to enable.
% Query the last warning to acquire the identifier. For example:
% warnStruct = warning('query', 'last')
% messageID = warnStruct.identifier
% messageID =
% MATLAB:concatenation:integerInteraction
% Turn off this warning "Warning: Image is too big to fit on screen; displaying at 33% "
warning('off', 'Images:initSize:adjustingMag');
% Get rid of warning about directory already existing:
% "Warning: Directory already exists."
warning('off', 'MATLAB:MKDIR:DirectoryExists');
% Turn off note "Warning: Added specified worksheet." that appears in the command window.
warning('off', 'MATLAB:xlswrite:AddSheet');
% Get rid of warning about roipolyold being deprecated:
% "Warning: Function ROIPOLYOLD will be removed in the future. Use ROIPOLY instead"
warning('off', 'images:removing:function');
% Get rid of warning about wavread() being deprecated:
% "Warning: WAVREAD will be removed in a future release. Use AUDIOREAD instead."
warning('off', 'MATLAB:audiovideo:wavread:functionToBeRemoved');
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
WarnUser(errorMessage);
end
return; % from TurnOffWarnings
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!