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
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.
Douglas Alves
Douglas Alves 2014-7-22
编辑:Douglas Alves 2014-7-22
The line is in ode45 309 which is "warning(message('MATLAB:ode45:IntegrationTolNotMet', sprintf( '%e', t ), sprintf( '%e', hmin )));" This happens once every each run it doesnt represent anything bad actually but it's annoying so I want to suppress it.

请先登录,再进行评论。

采纳的回答

Kelly Kearney
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 个评论
Douglas Alves
Douglas Alves 2014-7-22
Thank you very much Kelly that works... I just had to put 'off' in front of the message inside S. I guess it'`s because of the MATLAB version.
Kelly Kearney
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
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

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by