Code covered by the BSD License  

Highlights from
Test Tools - Utilities for unit tests

from Test Tools - Utilities for unit tests by Jay St. Pierre
Tools that facilitate debugging or writing unit test for MATLAB functions.

out=check_warn(fct_call, warn_string)
function out=check_warn(fct_call, warn_string)
%CHECK_WARN checks for an expected warning
%     Usage: CHECK_WARN(fct_call, warn_string).  The LASTWARN string will be
%     checked to see if the expected "warn_string" is a substring of LASTWARN,
%     and a nice "unit test" type pass/fail text will be output to the
%     screen.  The fct_call string is evaluated in the base workspace.

% $Source: /home/stpierre/cvsroot/matlab/tools/test_tools/check_warn.m,v $
% $Revision: 1.4 $
% $Date: 2009-07-26 20:41:24 $

% Copyright (c) 2000-2009, Jay A. St. Pierre.  All rights reserved.

if nargin~=2
  error('check_warn() requires two input arguments')
elseif ~ischar(fct_call)
  error('fct_call must be a string (it will be evaluated internally)')
elseif ~ischar(warn_string)
  error('warn_string must be a string')
end

dbclear if warning

disp(['Function Call: ', fct_call, 10])
disp('Expected Warning:')
disp([warn_string, 10])
lastwarn('');
evalin('base', fct_call);

if findstr(lastwarn, warn_string)
  disp('warning == expected_warning   ***PASSED***')
  disp(' ')
  out=0;
else
  disp('warning ~= expected_warning   ***FAILED***')
  disp(' ')
  lastwarn
  disp(' ')
  warn_string %#ok<NOPRT>
  disp(' ')
  out=1;
end

Contact us at files@mathworks.com