function padStr = padWithZeros(numDigits,padNum)
%padStr = padWithZeros(numDigits, padNum)
%==========================================================================
% Add leading zeros to a number and return a character string. This is 
% mostly useful for working with file names, but for reading and writing.
%
% --- INPUT ---
% numDigits - int, total width of number (number of characters)
% padNum  - int, number to add padding zeros to
%
% --- OUTPUT ---
% padStr - string, padded with leading zeros to length of numDigits
%
% -------------------------------------------------------------------------
% History - Orion L. Kafka
% 12-11-2017 - written
%
%==========================================================================

pad = '';
for jj = numDigits:-1:1
    % Find the number of digits in the number
    if padNum < 10^(jj) && padNum >= 10^(jj-1)
        zerosToAdd = numDigits-jj;
        % add a zero for each digit short of the requested number of digits
        for kk = 1:zerosToAdd
            pad = [pad,'0'];
        end
    end
end
% combine the number and the padding zeros
padStr = [pad,num2str(padNum)];