Code covered by the BSD License  

Highlights from
Date Odometer class

image thumbnail

Date Odometer class

by

 

12 Apr 2011 (Updated )

Add an odometer-like object to a plot to show the progression of time.

break_into_digits(input_integer, pad_to_length)
function varargout = break_into_digits(input_integer, pad_to_length)
% Break an integer into thousand, hundreds, etc.
%
% digits_vector = break_into_digits(input_integer, [pad_to_length])
%
% Ex. break_into_digits(1979) returns [1, 9, 7, 9]
%
% Inputs:
%   input_integer       Integer to be analyzed.
%   pad_to_length       Optional. Force this number of digits, even if zero
%
% Output:
%   digits_vector       Input integer broken down into digits.

% Kevin J. Delaney, BMT Scientific Marine Services
% April 11, 2011

varargout{1} = [];

if ~exist('input_integer', 'var')
    help(mfilename);
    return
end

if isempty(input_integer) || ~isnumeric(input_integer)
    errordlg('Input "date_1" is empty or non-numeric.', mfilename);
    return
end

if ~iswhole(input_integer) || (input_integer < 0)
    errordlg('Input "input_integer" is not a positive integer.', mfilename);
    return
end

pad_digits =  exist('pad_to_length', 'var') && ...
              ~isempty(pad_to_length) && ...
              isnumeric(pad_to_length);

num_places = ceil(log10(input_integer));

if pad_digits
    if ~iswhole(pad_to_length) || (pad_to_length < 0)
        errordlg('Input "pad_to_length" is not a positive integer.', mfilename);
        return
    end
    
    num_places = max([pad_to_length, num_places]);
end

accumulated_value = 0;
digits_vector = zeros(1, num_places);

for index = 1:num_places
    this_exponent = num_places - index;
    this_tens_place = 10 ^ this_exponent;
    this_digit = floor((input_integer - accumulated_value) / this_tens_place);
    accumulated_value = accumulated_value + (this_digit * this_tens_place);
    digits_vector(index) = this_digit;
end

if nargout == 1
    varargout{1} = digits_vector;
else
    for k = 1:length(digits_vector)
        varargout{k} = digits_vector(k);
    end
end

Contact us