function DateNumber = m2xdate(MATLABDateNumber, Convention)
%M2XDATE MATLAB Serial Date Number Form to Excel Serial Date Number Form.
%
%   DateNumber = m2xdate(MATLABDateNumber, Convention)
%
%   Summary: This function converts serial date numbers from the MATLAB
%            serial date number format to the Excel serial date number format.
%
%   Inputs: MATLABDateNumber - Nx1 or 1xN vector of serial date numbers in
%           MATLAB serial date number form
%           Convention - Nx1 or 1xN vector or scalar flag value indicating
%           which Excel serial date number convention should be used when
%           converting from MATLAB serial date numbers; possible values are:         
%           a) Convention = 0 - 1900 date system in which a serial date
%              number of one corresponds to the date 1-Jan-1900 (default).
%              Note: Excel erroneously treats the year 1900 as a leap year,
%              which we account for here.
%           b) Convention = 1 - 1904 date system in which a serial date
%                number of zero corresponds to the date 1-Jan-1904
%
%   Outputs: Nx1 or 1xN vector of serial date numbers in Excel serial date
%            number form.
%
% 
%   Example: StartDate = 729706
%            Convention = 0;
%
%            EndDate = m2xdate(StartDate, Convention);
%
%            returns:
%
%            EndDate = 35746
%
%   See also X2MDATE.

%   Copyright 1995-2012 The MathWorks, Inc.

% Check for empty dates
if isempty(MATLABDateNumber)
    DateNumber = MATLABDateNumber;
    return   
end

% Convert date strings to serial date numbers if necessary
if any(ischar(MATLABDateNumber(:)))
     MATLABDateNumber = datenum(MATLABDateNumber);
end

% Check the number of arguments in and set defaults
if nargin < 2
     Convention = zeros(size(MATLABDateNumber));
end

% Make sure input date numbers are positive
if any(MATLABDateNumber(:) <= 0)
     error(message('finance:m2xdate:inputsMustBePositive'))
end

% Do any needed scalar expansion on the convention flag and parse
if any(size(Convention) ~= size(MATLABDateNumber)) && (max(size(Convention)) ~= 1)
     error(message('finance:m2xdate:invalidConventionSize'))
elseif length(Convention(:)) == 1
     Convention = Convention * ones(size(MATLABDateNumber));
end

if any(Convention ~= 0 & Convention ~= 1)
     error(message('finance:m2xdate:invalidConvention'))
end


% Get the shape of the input for later reshaping of the output
[RowSize, ColumnSize] = size(MATLABDateNumber);

% Set conversion factor for both (1900 & 1904) date systems
X2MATLAB1900 = 693961;
X2MATLAB1904 = 695422;

% Convert to the Excel serial date number
actual1900Idx = (Convention == 0) & (MATLABDateNumber < 694021);
if any(actual1900Idx(:))
     Temp(actual1900Idx) = MATLABDateNumber(actual1900Idx) - X2MATLAB1900;
end

% Excel erroneously believes 1900 was a leap year, so after February 28,
% 1900, we adjust to account for this.
corrected1900Idx = (Convention == 0) & (MATLABDateNumber >= 694021);
if any(corrected1900Idx(:))
    Temp(corrected1900Idx) = MATLABDateNumber(corrected1900Idx) - X2MATLAB1900 + 1;
end

X1904Ind = (Convention == 1);
if any(X1904Ind(:))
     Temp(X1904Ind) = MATLABDateNumber(X1904Ind) - X2MATLAB1904;
end

% Reshape the output
DateNumber = reshape(Temp, RowSize, ColumnSize);
