File: psychassert.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (51 lines) | stat: -rw-r--r-- 1,694 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function psychassert(varargin)
% psychassert(expression, ...) - Replacement for Matlab 7 builtin assert().
% This is hopefully useful for older Matlab installations and
% for the Octave port:
%
% If the assert-function is supported as builtin function on
% your Matlab installation, this function will call the builtin
% "real" assert function. Read the "help assert" for usage info.
%
% If your Matlab lacks a assert-function, this function
% will try to emulate the real assert function. The only known limitation
% of our own assert wrt. Matlabs assert is that it can't handle the MSG_ID
% parameter as 2nd argument. Passing only message strings or message
% formatting strings + variable number of arguments should work.
%

% History:
% 01/06/09 mk Wrote it. Based on the specification of assert in Matlab 7.3.

if exist('assert', 'builtin')==5
  % Call builtin implementation:
  builtin('assert', varargin{:});
else
  % Use our fallback-implementation:
  if nargin < 1
      error('Not enough input arguments.');
  else
      expression = varargin{1};
      if ~isscalar(expression) || ~islogical(expression)
          error('The condition input argument must be a scalar logical.');
      end
      
      % Expression true?
      if ~expression
          % Assertion failed:
          if nargin < 2
              error('Assertion failed.');
          else
              if nargin < 3
                  emsg = sprintf('%s\n', varargin{2});
                  error(emsg); %#ok<SPERR>
              else
                  emsg = sprintf(varargin{2}, varargin{3:end});
                  error(emsg); %#ok<SPERR>                  
              end
          end
      end
  end
end

return;