?? vrassert.m
字號:
function vrassert(varargin)%vrassert(testCondition)% Evaluates the test condition in the caller's context. If the result is% false, an error is thrown indicating that the test failed.% TESTCONDITION should be a string (Matlab 2007a has a real ASSERT operator,% but many people don't have that version, so that's why we have this% function).%%EXAMPLES:% vrassert('1==2') ==> assertion error generated% x=1; vrassert('x==1') ==> no error% vrassert(1==2) ==> bad parameter error generated% vrassert('{a==3') ==> error generated by Matlab (bad vrassert code)%%by Gerald Dalleyfor i=1:nargin if (~ischar(varargin{i})) error('Type HELP ASSERT for usage'); endendtestCondition = join(' ', varargin);if (~evalin('caller', testCondition)) error(['ASSERT FAILED: ' testCondition]);end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function s = join(d,varargin)% This JOIN function is an inlined version of Gerald Dalley's one posted at the% Matlab Central website. It is placed here as a convenience to users that% have not downloaded it.if (isempty(varargin)), s = '';else if (iscell(varargin{1})) s = join(d, varargin{1}{:}); else s = varargin{1}; end for ss = 2:length(varargin) s = [s d join(d, varargin{ss})]; %#ok<AGROW> endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function test %#ok<DEFNU>% Copy-and-paste the below code into the command window to test this% function.clear all;worked = 0; try vrassert('1==2'); catch worked = 1; endif (~worked), error('Test failed'); endworked = 1; try x=1; vrassert('x==1'); catch worked = 0; endif (~worked), error('Test failed'); endworked = 0; try vrassert(1==2); catch worked = 1; endif (~worked), error('Test failed'); endworked = 0; try vrassert('{a==3'); catch worked = 1; endif (~worked), error('Test failed'); end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -