Using try/catch to get warning message

41 ビュー (過去 30 日間)
Douglas Alves
Douglas Alves 2014 年 7 月 22 日
コメント済み: Kelly Kearney 2014 年 7 月 22 日
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 Center および File ExchangeDesktop についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by