function newy = table1(tab, newx)
%
% newy = table1(tab, newx)
% 
% TABLE1 1-D Table look-up.
%    Y = TABLE1(TAB,X0) returns a table of linearly interpolated rows
%    from table TAB, looking up X0 in the first column of TAB.  TAB is
%    a matrix that contains key values in its first column and data in
%    the remaining columns.  One interpolated row from TAB will be
%    returned for each element in X0.  The first column of TAB must be
%    monotonic.
%
% comments:
%
% This is a wrapper for a mex file, "table1_core", which
% used to be called table1.   Examples of speed 
%
% function    time            comment
% --------    ------          --------
% table1     0.5 sec         (mex)
% interp1    1.1 sec
% interp1q   2.6 sec         ("quick interp1")
% table1    (minutes)        (Mathworks, obsolete)
%
%
% This wrapper is being added because the table1_core 
% mex file version invokes a matlab bug.  The result 
% under some versions of Matlab) is that the program
% bombs out and takes the entire matlab session with it. 
% The bug is in the error message handler and has to do 
% with library incompatinbilities.  The real 'fix' is here:
% 
% http://www.mathworks.com/support/solutions/data/1-QBCS1.html?solution=1-QBCS1
%


if length(size(tab)) ~= 2
    error('input table must be a 2-dimensional array.\n')
end


if length(size(tab)) > 2
    error(' new x vector must be one-demensional array.\n')
end


% get sizes
[nrow, ncol] = size(tab);
[newx_nrow, newx_ncol] = size(newx);

if nrow <= 1
    error('Need more than one row in the table.\n')
end

if ncol < 2
    error('Need at least two columns in the table.\n')
end


if min(diff(tab(:,1))) <= 0
    error('Table is not monotonic.\n')
end

if nargin ~= 2
    error('Need 2 input arguments.\n')
end

if nargout > 1
      error('Only 1 output argument permitted.\n')
end


if newx_nrow > 1 & newx_ncol > 1
    error('Need single row or column in the new x vector.\n')
end


% now run it
newy = table1_core(tab, newx);