function data = ...
                 cellRangeToTyped(dataCells,rowRange,colRange,conversion)
% Expect dataCells to be a 2d cell array of strings; convert a
% rectangular range into another data format. (This function stems
% from our need to read certain mixed CSV-type files exported from
% collaborators' industrial systems. It is probably not very useful
% for general purposes.)
% 
% rowRange - index vector indicating which rows to convert
% colRange - index vector indicating which columns to convert
% conversion - one of: 
%
%               * 'string' - no conversion, only select the range
%               * 'double' - convert to floating point
%               * 'doubleFi' - convert to floating point, ',' as the
%                              decimal separator
%               * 'int'    - convert to integer
%               * 'date1'  - date from a MM.DD.YY text format
%               * 'date2'  - date from a MM.DD.YYYY text format
%               * 'time1'  - time of day from a HH:MM (24h) text format
% 


for outR = 1:numel(rowRange)
    for outC = 1:numel(colRange)
        iR = rowRange(outR);
        iC = colRange(outC);
        
        dataStr = deblank(dataCells{iR,iC});
        
        
        if strcmp(conversion,'string')
            data{outR,outC} = dataStr;
        elseif strcmp(conversion,'int')  % 'num' sufficient? Is
                                         % same function anyway
            data(outR,outC) = str2num(dataStr);
        elseif strcmp(conversion,'double')
            if isempty(dataStr), data(outR,outC) = NaN; continue, end;
            data(outR,outC) = str2num(dataStr);
        elseif strcmp(conversion,'doubleFi')
            dataStr = strrep(dataStr,',','.');
            if isempty(dataStr), data(outR,outC) = NaN; continue, end;
            data(outR,outC) = str2num(dataStr);
        elseif strcmp(conversion,'date1')
            data(outR,outC) = datenum(dataStr,'dd.mm.yy');
        elseif strcmp(conversion,'date2')
            data(outR,outC) = datenum(dataStr,'dd.mm.yyyy');
        elseif strcmp(conversion,'time1')
            if isempty(dataStr), data(outR,outC) = NaN; continue, end;
            data(outR,outC) = datenum(dataStr,'HH:MM') - datenum('00:00','HH:MM');
        elseif strcmp(conversion,'time2')
            if isempty(dataStr), data(outR,outC) = NaN;
            else
                data(outR,outC) = datenum(dataStr,'HH:MM:SS') - datenum('00:00:00','HH:MM:SS');
            end
        else
            error('Conversion type not supported: ''%s''',conversion);
        end
    end
end

