function [matrixResult, cellResult] = xlsread(filename, arg)
%XLSREAD read Excel spreadsheet (XLS) file.
%   A = XLSREAD('FILENAME') returns numeric data from first sheet in
%   Excel file FILENAME.
%
%   [A, B] = XLSREAD('FILENAME') numeric data is stored in A, text data
%   is stored in B.
%
%   [...] = XLSREAD('FILENAME','SHEETNAME') same as above but uses sheet
%   named SHEETNAME.  It is an error if SHEETNAME does not exist.
%
%   Empty cells or cells with text create NaNs in the numeric result.
%
%   Leading rows and columns of text do not create NaNs in the numeric
%   result.  This makes it easier to load files that look like this:
%
%   Time   Temp
%    12      98
%    13      99
%    14      97
%
%   City    Time   Temp
%   Dallas   12      98
%   Tulsa    13      99
%   Boise    14      97
%
%   To get available sheet names from file, call XLSFINO with two outputs.
%   For example:
%
%   [status, sheetNames] = xlsfinfo(FILENAME)
%
%   When reading date fields from Excel files, the date numbers need
%   conversion to a MATLAB date.  For example, if your Excel file contains:
%
%   4/12/99
%   4/13/99
%   4/14/99
%
%   Convert the data to MATLAB dates like this:
%
%   excelDates = xlsread(FILENAME)
%   matlabDates = datenum('30-Dec-1899') + excelDates
%
%   See also LOAD, FILEFORMATS, XLSFINFO, DATENUM, DATEVEC

%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.19 $  $Date: 2000/08/24 06:11:53 $


% validate input and output args
error(nargchk(1,2,nargin));
error(nargoutchk(0,2,nargout));

% Get Filename
if ~isstr(filename), error('Filename must be a string.'); end

% do some validation
if isempty(filename), error('Filename must not be empty.'); end

% put extension on
if all(filename~='.'), filename = [filename '.xls']; end

% look for file on path
if exist(filename,'file') ~= 2,  error('File not found'); end

% locate file on path if name is not qualified
if isempty(fileparts(filename))
    filename = which(filename);
end

biffvector = biffread(filename);

%matrixResult = biffvector;
%return;

% get sheet names
[data, names] = biffparse(biffvector);

% if the names array is empty, this is an old style biff record with 
% no sheet name.  just return data and empty.
if isempty(names) & nargin == 1
    matrixResult = data;
    cellResult = names;
    return;
end

if nargin == 1
    % just get the first sheet
    [n, s] = biffparse(biffvector, names{1});
    [matrixResult, cellResult] = trimitup(n,s);
else
    % try to read this sheet
    [n, s] = biffparse(biffvector, arg);
    [matrixResult, cellResult] = trimitup(n,s);
end

function [n,s] = trimitup(n,s)
% trim leading rows or cols
% if the string result has dimensions corresponding to a column or row of
% zeros in the matrix result, trim the zeros.
if ~isempty(n) & ~isempty(s)
    [mn, nn] = size(n);
    [ms, ns] = size(s);

    if ms == mn
        % trim leading column(s) from numeric data
        while all( isnan(n(:,1)) )
            n = n(:,2:end);
        end
    end

    if ns == nn
        % trim leading row(s) from numeric data
        while all( isnan(n(1,:)) )
            n = n(2:end,:);
        end
    end
end
                
