function [time, timeFormat]=getTimeVector(h,SelectedColumns,SelectedRows)
% The function will return the time vector based on the dataset selected by
% the user in the import dialog.  If the time vector is in absolute time
% format (e.g. hh:mm:ss), the return value will be a cell column with each
% cell as a time string.  If the time vector is in relative time format
% with unit (e.g. 1 sec), the return value will be a double column.  If the
% selected values are not valid, the return value is empty.   

if ~isempty(h.Handles.ActiveX)
    % if using ActiveX web component
    % get active workbook
    eActiveWorkbook=h.Handles.ActiveX.ActiveWorkbook;
    % get all the worksheets
    eSheets = eActiveWorkbook.Sheets;

    % get sheet combo selection
    ind=get(h.Handles.COMBtimeSheet,'Value');
    if ind>length(h.IOData.DES)
        % the time vector is selected from a different excel file, not the
        % one shown in the data section.  Another dialog will popup.
        ;
    else
        % the time vector is selected from the same excel file and user can
        % select the sheet containing the time vector from the combobox
        tmpSheet=Item(eSheets,h.IOData.DES{ind});
        if get(h.Handles.RADIOtimeColumn,'Value')==1
            % the time vector is stored in a column
            % get the available column headings into a cell array
            tmpColStr=get(h.Handles.COMBtimeColumn,'String');
            % get value from each cell selected
            if get(h.Handles.RADIOsampleCOLUMN,'Value')==1
                % a data sample is a column and the number of the selected
                % columns are the number of the time stamps to collect
                h.checkTimeFormat(h.IOData.DES{ind},'column',get(h.Handles.COMBtimeColumn,'Value'));
                if getfield(h.IOData.formatcell,'columnIsAbsTime')>=0;
                    % the length of the time vector is the length of the
                    % selected columns in the data sheet
                    time=cell(length(SelectedColumns),1);
                    % time vector is the first column and using the
                    % absolute format, a.k.a, using date/time string
                    for i=1:length(SelectedColumns)
                        tmpCell = [tmpColStr{get(h.Handles.COMBtimeColumn,'Value')} num2str(SelectedColumns(i))];
                        tmpRange = tmpSheet.Range(tmpCell);
                        tmpTime=get(tmpRange, 'Value');
                        if (isnumeric(tmpTime) && isnan(tmpTime)) || ...
                            isempty(h.IsTimeFormat(h.IOData.formatcell.columnFormat,{tmpTime}))
                            %errordlg('Invalid time vector based on your current selection.  Please select again.');
                            time=[];
                            timeFormat=[];
                            return;
                        else
                            time(i)={datestr(tmpTime,getfield(h.IOData.formatcell,'columnIsAbsTime'))};
                        end
                    end
                    if ~isempty(time)
                        set(tmpSheet.Range([tmpColStr{get(h.Handles.COMBtimeColumn,'Value')} num2str(SelectedColumns(1)) ':' tmpColStr{get(h.Handles.COMBtimeColumn,'Value')} num2str(SelectedColumns(end))]), 'Value', time);
                        timeFormat=getfield(h.IOData.formatcell,'columnIsAbsTime');
                    else
                        time=[];
                        timeFormat=[];
                    end
                else
                    % the length of the time vector is the length of the
                    % selected columns in the data sheet
                    time=zeros(length(SelectedColumns),1);
                    % either not the first column or the time is not in the
                    % absolute format, a.k.a, using date/time string
                    for i=1:length(SelectedColumns)
                        tmpCell = [tmpColStr{get(h.Handles.COMBtimeColumn,'Value')} num2str(SelectedColumns(i))];
                        tmpRange = tmpSheet.Range(tmpCell);
                        tmpTime=get(tmpRange, 'Value');
                        if isnumeric(tmpTime) && ~isnan(tmpTime)
                            time(i)=tmpTime;
                        else
                            %errordlg('Invalid time vector based on your current selection.  Please select again.');
                            time=[];
                            timeFormat=[];
                            return;
                        end
                    end
                    if ~isempty(time)
                        timeFormat=-1;
                    else
                        time=[];
                        timeFormat=[];
                    end
                end
            else
                % a data sample is a row and the number of the selected rows
                % are the number of the time stamps to collect
                h.checkTimeFormat(h.IOData.DES{ind},'column',get(h.Handles.COMBtimeColumn,'Value'));
                if getfield(h.IOData.formatcell,'columnIsAbsTime')>=0
                    % the length of the time vector is the length of the
                    % selected rows in the data sheet
                    time=cell(length(SelectedRows),1);
                    % time vector is the first column and using the
                    % absolute format, a.k.a, using date/time string
                    for i=1:length(SelectedRows)
                        tmpCell = [tmpColStr{get(h.Handles.COMBtimeColumn,'Value')} num2str(SelectedRows(i))];
                        tmpRange = tmpSheet.Range(tmpCell);
                        tmpTime=get(tmpRange, 'Value');
                        if (isnumeric(tmpTime) && isnan(tmpTime)) || ...
                            isempty(h.IsTimeFormat(h.IOData.formatcell.rowFormat,{tmpTime}))
                            %errordlg('Invalid time vector based on your current selection.  Please select again.');
                            time=[];
                            timeFormat=[];
                            return;
                        else
                            time(i)={datestr(tmpTime,getfield(h.IOData.formatcell,'columnIsAbsTime'))};
                        end
                    end
                    if ~isempty(time)
                        set(tmpSheet.Range([tmpColStr{get(h.Handles.COMBtimeColumn,'Value')} num2str(SelectedRows(1)) ':' tmpColStr{get(h.Handles.COMBtimeColumn,'Value')} num2str(SelectedRows(end))]), 'Value', time);
                        timeFormat=getfield(h.IOData.formatcell,'columnIsAbsTime');
                    else
                        time=[];
                        timeFormat=[];
                    end
                else
                    % the length of the time vector is the length of the
                    % selected rows in the data sheet
                    time=zeros(length(SelectedRows),1);
                    for i=1:length(SelectedRows)
                        tmpCell = [tmpColStr{get(h.Handles.COMBtimeColumn,'Value')} num2str(SelectedRows(i))];
                        tmpRange = tmpSheet.Range(tmpCell);
                        tmpTime=get(tmpRange, 'Value');
                        if isnumeric(tmpTime) && ~isnan(tmpTime)
                            time(i)=tmpTime;
                        else
                            %errordlg('Invalid time vector based on your current selection.  Please select again.');
                            time=[];
                            timeFormat=[];
                            return;
                        end
                    end
                    if ~isempty(time)
                        timeFormat=-1;
                    else
                        time=[];
                        timeFormat=[];
                    end
                end
            end
        else
            % the time vector is stored in a row
            % get the available row numbers into a cell array
            tmpRowStr=get(h.Handles.COMBtimeRow,'String');
            % get value from each cell selected
            if get(h.Handles.RADIOsampleCOLUMN,'Value')==1
                % a data sample is a column and the number of the selected
                % columns are the number of the time stamps to collect
                h.checkTimeFormat(h.IOData.DES{ind},'row',get(h.Handles.COMBtimeRow,'Value'));
                if getfield(h.IOData.formatcell,'rowIsAbsTime')>=0
                    % the length of the time vector is the length of the
                    % selected columns in the data sheet
                    time=cell(length(SelectedColumns),1);
                    % time vector is the first row and using the
                    % absolute format, a.k.a, using date/time string
                    for i=1:length(SelectedColumns)
                        tmpCell = [h.findcolumnletter(SelectedColumns(i)) tmpRowStr{get(h.Handles.COMBtimeRow,'Value')}];
                        tmpRange = tmpSheet.Range(tmpCell);
                        tmpTime=get(tmpRange, 'Value');
                        if (isnumeric(tmpTime) && isnan(tmpTime)) || ...
                            isempty(h.IsTimeFormat(h.IOData.formatcell.rowFormat(SelectedColumns(i)),{tmpTime}))
                            %errordlg('Invalid time vector based on your current selection.  Please select again.');
                            time=[];
                            timeFormat=[];
                            return;
                        else
                            time(i)={datestr(tmpTime,getfield(h.IOData.formatcell,'rowIsAbsTime'))};
                        end
                    end
                    if ~isempty(time)
                        set(tmpSheet.Range([h.findcolumnletter(SelectedColumns(1)) tmpRowStr{get(h.Handles.COMBtimeRow,'Value')} ':' h.findcolumnletter(SelectedColumns(end)) tmpRowStr{get(h.Handles.COMBtimeRow,'Value')}]), 'Value', time);                
                        timeFormat=getfield(h.IOData.formatcell,'rowIsAbsTime');
                    else
                        time=[];
                        timeFormat=[];
                    end
                else
                    % the length of the time vector is the length of the
                    % selected columns in the data sheet
                    time=zeros(length(SelectedColumns),1);
                    % either not the first column or the time is not in the
                    % absolute format, a.k.a, using date/time string
                    for i=1:length(SelectedColumns)
                        tmpCell = [h.findcolumnletter(SelectedColumns(i)) tmpRowStr{get(h.Handles.COMBtimeRow,'Value')} ];
                        tmpRange = tmpSheet.Range(tmpCell);
                        tmpTime=get(tmpRange, 'Value');
                        if isnumeric(tmpTime) && ~isnan(tmpTime)
                            time(i)=tmpTime;
                        else
                            %errordlg('Invalid time vector based on your current selection.  Please select again.');
                            time=[];
                            timeFormat=[];
                            return;
                        end
                    end
                    if ~isempty(time)
                        timeFormat=-1;
                    else
                        time=[];
                        timeFormat=[];
                    end
                end
            else
                % a data sample is a row and the number of the selected rows
                % are the number of the time stamps to collect
                h.checkTimeFormat(h.IOData.DES{ind},'row',get(h.Handles.COMBtimeRow,'Value'));
                if getfield(h.IOData.formatcell,'rowIsAbsTime')>=0
                    % the length of the time vector is the length of the
                    % selected rows in the data sheet
                    time=cell(length(SelectedRows),1);
                    % time vector is the first row and using the
                    % absolute format, a.k.a, using date/time string
                    for i=1:length(SelectedRows)
                        tmpCell = [h.findcolumnletter(SelectedRows(i)) tmpRowStr{get(h.Handles.COMBtimeRow,'Value')}];
                        tmpRange = tmpSheet.Range(tmpCell);
                        tmpTime=get(tmpRange, 'Value');
                        if (isnumeric(tmpTime) && isnan(tmpTime)) || ...
                            isempty(h.IsTimeFormat(h.IOData.formatcell.rowFormat(SelectedRows(i)),{tmpTime}))
                            %errordlg('Invalid time vector based on your current selection.  Please select again.');
                            time=[];
                            timeFormat=[];
                            return;
                        else
                            time(i)={datestr(tmpTime,getfield(h.IOData.formatcell,'rowIsAbsTime'))};
                        end
                    end
                    if ~isempty(time)
                        set(tmpSheet.Range([h.findcolumnletter(SelectedRows(1)) tmpRowStr{get(h.Handles.COMBtimeRow,'Value')} ':' h.findcolumnletter(SelectedRows(end)) tmpRowStr{get(h.Handles.COMBtimeRow,'Value')}]), 'Value', time);                
                        timeFormat=getfield(h.IOData.formatcell,'rowIsAbsTime');
                    else
                        time=[];
                        timeFormat=[];
                    end
                else
                    % the length of the time vector is the length of the
                    % selected rows in the data sheet
                    time=zeros(length(SelectedRows),1);
                    for i=1:length(SelectedRows)
                        tmpCell = [h.findcolumnletter(SelectedRows(i)) tmpRowStr{get(h.Handles.COMBtimeRow,'Value')} ];
                        tmpRange = tmpSheet.Range(tmpCell);
                        tmpTime=get(tmpRange, 'Value');
                        if isnumeric(tmpTime) && ~isnan(tmpTime)
                            time(i)=tmpTime;
                        else
                            %errordlg('Invalid time vector based on your current selection.  Please select again.');
                            time=[];
                            timeFormat=[];
                            return;
                        end
                    end
                    if ~isempty(time)
                        timeFormat=-1;
                    else
                        time=[];
                        timeFormat=[];
                    end
                end
            end
        end
    end
end

if ~isempty(h.Handles.tsTable)
    % if using uitable to display
    % get sheet combo selection
    ind=get(h.Handles.COMBtimeSheet,'Value');
    if ind>length(h.IOData.DES)
        % the time vector is selected from a different excel file, not the
        % one shown in the data section.  Another dialog will popup.
        ;
    else
        tmpSheet=getfield(h.IOData.rawdata,h.IOData.DES{get(h.Handles.COMBtimeSheet,'Value')});
        % the time vector is selected from the same excel file and user can
        % select the sheet containing the time vector from the combobox
        if get(h.Handles.RADIOtimeColumn,'Value')==1
            % the time vector is stored in a column
            % get the available column headings into a cell array
            tmpColStr=get(h.Handles.COMBtimeColumn,'String');
            % get value from each cell selected
            if get(h.Handles.RADIOsampleCOLUMN,'Value')==1
                % a data sample is a column and the number of the selected
                % columns are the number of the time stamps to collect
                % the length of the time vector is the length of the
                % selected columns in the data sheet
                time=zeros(length(SelectedColumns),1);
                % either not the first column or the time is not in the
                % absolute format, a.k.a, using date/time string
                for i=1:length(SelectedColumns)
                    try
                        tmpTime=tmpSheet{SelectedColumns(i),get(h.Handles.COMBtimeColumn,'Value')};
                    catch
                        time=[];
                        timeFormat=[];
                        return;
                    end                        
                    if isnumeric(tmpTime) && ~isnan(tmpTime)
                        time(i)=tmpTime;
                        timeFormat=-1;
                    else
                        %errordlg('Invalid time vector based on your current selection.  Please select again.');
                        time=[];
                        timeFormat=[];
                        return;
                    end
                end
            else
                % a data sample is a row and the number of the selected rows
                % are the number of the time stamps to collect
                % the length of the time vector is the length of the
                % selected rows in the data sheet
                time=zeros(length(SelectedRows),1);
                for i=1:length(SelectedRows)
                    try
                        tmpTime=tmpSheet{SelectedRows(i),get(h.Handles.COMBtimeColumn,'Value')};
                    catch
                        time=[];
                        timeFormat=[];
                        return;
                    end                        
                    if isnumeric(tmpTime) && ~isnan(tmpTime)
                        time(i)=tmpTime;
                        timeFormat=-1;
                    else
                        %errordlg('Invalid time vector based on your current selection.  Please select again.');
                        time=[];
                        timeFormat=[];
                        return;
                    end
                end
            end
        else
            % the time vector is stored in a row
            % get the available row numbers into a cell array
            tmpRowStr=get(h.Handles.COMBtimeRow,'String');
            % get value from each cell selected
            if get(h.Handles.RADIOsampleCOLUMN,'Value')==1
                % a data sample is a column and the number of the selected
                % columns are the number of the time stamps to collect
                % the length of the time vector is the length of the
                % selected columns in the data sheet
                time=zeros(length(SelectedColumns),1);
                % either not the first column or the time is not in the
                % absolute format, a.k.a, using date/time string
                for i=1:length(SelectedColumns)
                    try
                        tmpTime=tmpSheet{get(h.Handles.COMBtimeRow,'Value'),SelectedColumns(i)};
                    catch
                        time=[];
                        timeFormat=[];
                        return;
                    end                        
                    if isnumeric(tmpTime) && ~isnan(tmpTime)
                        time(i)=tmpTime;
                        timeFormat=-1;
                    else
                        %errordlg('Invalid time vector based on your current selection.  Please select again.');
                        time=[];
                        timeFormat=[];
                        return;
                    end
                end
            else
                % a data sample is a row and the number of the selected rows
                % are the number of the time stamps to collect
                % the length of the time vector is the length of the
                % selected rows in the data sheet
                time=zeros(length(SelectedRows),1);
                for i=1:length(SelectedRows)
                    try
                        tmpTime=tmpSheet{get(h.Handles.COMBtimeRow,'Value'),SelectedRows(i)};
                    catch
                        time=[];
                        timeFormat=[];
                        return;
                    end                        
                    if isnumeric(tmpTime) && ~isnan(tmpTime)
                        time(i)=tmpTime;
                        timeFormat=-1;
                    else
                        %errordlg('Invalid time vector based on your current selection.  Please select again.');
                        time=[];
                        timeFormat=[];
                        return;
                    end
                end
            end
        end
    end
end