function flag=savets(h)
% SAVETS imports the selected time and data into tstool

% this method is used to create one or more timeseries objects based on the
% user selections in the data import dialog.  If the time vector is in
% absolute date/time format, it will be automatically converted into proper
% MATLAB-supported date/time format.

% Author: Rong Chen 
% Revised: 
% Copyright 1986-2004 The MathWorks, Inc.

% -------------------------------------------------------------------------
% pre-save check
% -------------------------------------------------------------------------
if isempty(h.IOData.SelectedRows) || isempty(h.IOData.SelectedColumns)
    errordlg('No data block has been selected.  To select data, go back to Step 2.','Time Series Tools');
    flag=false;
    return;
end
ButtonSameName=[];
if ~isempty(h.IOData.SelectedVariableInfo)
    tmp=load(h.IOData.FileName,h.IOData.SelectedVariableInfo.name);
    dataOriginal=tmp.(h.IOData.SelectedVariableInfo.name);
else
    flag=false;
    return;
end
if get(h.Parent.Handles.PNLoption,'SelectedObject')==h.Parent.Handles.RADIOsingleNEW
    % create a new timeseries object
    % get the timeseries object name from the edit box
    UserInputName=strtrim(get(h.Parent.Handles.EDTsingleNEW,'String'));
    if ~isvarname(UserInputName)
        errordlg('This is not a valid MATLAB variable name.','Time Series Tools');
        flag=false;
        return
    end
    set(h.Parent.Handles.EDTsingleNEW,'String',UserInputName);
    % check if there exists a time series object with the same name
    tsgui = tstool;
    alltsobjects = get(tsgui.Tsnode.getChildren,{'Timeseries'});
    if length(alltsobjects)>0
        for i=1:length(alltsobjects)
            if strcmp({alltsobjects{i}.Name},UserInputName)
                ButtonSameName=questdlg('A time series object with the same name already exists in tstool.  Do you want to replace its contents with the current selection?', ...
                                'Time Series Tools', 'Replace', 'Abort', 'Replace');                
                drawnow;
                switch ButtonSameName
                    case 'Replace'
                        % delete the old time series object in tstool
                        ;
                    case 'Abort'                    
                        flag=false;
                        return;
                end
            end                            
        end
    end
elseif get(h.Parent.Handles.PNLoption,'SelectedObject')==h.Parent.Handles.RADIOsingleINSERT
    % import into an existing timeseries object with name given
    % in the combo box
    % get the name
    str=get(h.Parent.Handles.COMBsingleINSERT,'String');
    UserInputName=str{get(h.Parent.Handles.COMBsingleINSERT,'Value')};
else
    if get(h.Parent.Handles.COMBmultipleNEW,'Value')==1
        % use strings in a row or column as timeseries names
        try
            UserInputName=str2double(strtrim(get(h.Parent.Handles.EDTmultipleNEW,'String')));
        catch
            errordlg('The row index for the row containing variable names should be an integer.','Time Series Tools');
            flag=false;
            return
        end
    else
        % use the given name + suffix as timeseries names
        UserInputName=strtrim(get(h.Parent.Handles.EDTmultipleNEW,'String'));
        if ~isvarname(UserInputName)
            errordlg('The common part of the variable name should be a valid MATLAB variable name.','Time Series Tools');
            flag=false;
            return
        end
    end
end

h.Handles.bar=waitbar(10/100,'Importing Time Series Object(s), Please Wait...','WindowStyle','modal');

% -------------------------------------------------------------------------
% get time vector and remove blank points
% -------------------------------------------------------------------------
ind=get(h.Handles.COMBtimeSource,'Value');
if ind==1
    % from the same workspace variable
    % default
    SelectedRows=h.IOData.SelectedRows;
    SelectedColumns=h.IOData.SelectedColumns;
    % check blank time points, remove them if required, and get 'time' and
    % 'timeFormat'
    if get(h.Handles.COMBdataSample,'Value')==1
        % time vector is stored as a column
        tmpColStr=get(h.Handles.COMBtimeIndex,'String');
        ColStrValue=str2double(tmpColStr{get(h.Handles.COMBtimeIndex,'Value')});
        timeValue=dataOriginal(h.IOData.SelectedRows,ColStrValue);
        [SelectedIndex,flag]=h.checkBlankTimePoint(timeValue,'column');
        if ~flag
            delete(h.Handles.bar);
            return;
        end
        SelectedRows=h.IOData.SelectedRows(SelectedIndex);
        SelectedColumns=h.IOData.SelectedColumns(find(h.IOData.SelectedColumns~=get(h.Handles.COMBtimeIndex,'Value')));            
        time=timeValue(SelectedIndex);
        h.checkTimeFormat(h.IOData.SelectedVariableInfo.name,'column',tmpColStr{get(h.Handles.COMBtimeIndex,'Value')});
        % replace the stored time format with the selected time format
        % timeFormat=h.IOData.formatcell.columnIsAbsTime;
        [time, timeFormat]=getTimeColumn(h,SelectedRows);
        if timeFormat>=0
            timeFormat=h.IOData.formatcell.matlabFormatIndex(get(h.Handles.COMBtimeSheetFormat,'Value'));
            %time=datestr(time,timeFormat);
            %time=mat2cell(time,ones(1,size(time,1)),size(time,2));
        end
    else
        % time vector is stored as a row
        tmpRowStr=get(h.Handles.COMBtimeIndex,'String');
        RowStrValue=str2double(tmpRowStr{get(h.Handles.COMBtimeIndex,'Value')});
        timeValue=dataOriginal(RowStrValue,h.IOData.SelectedColumns);
        [SelectedIndex,flag]=h.checkBlankTimePoint(timeValue,'row');
        if ~flag
            delete(h.Handles.bar);
            return;
        end
        SelectedColumns=h.IOData.SelectedColumns(SelectedIndex);
        SelectedRows=h.IOData.SelectedRows(find(h.IOData.SelectedRows~=get(h.Handles.COMBtimeIndex,'Value')));            
        time=timeValue(SelectedIndex);
        h.checkTimeFormat(h.IOData.SelectedVariableInfo.name,'row',tmpRowStr{get(h.Handles.COMBtimeIndex,'Value')});
        % replace the stored time format with the selected time format
        % timeFormat=h.IOData.formatcell.rowIsAbsTime;
        [time,timeFormat]=h.getTimeRow(h.IOData.SelectedColumns);
        if timeFormat>=0
            timeFormat=h.IOData.formatcell.matlabFormatIndex(get(h.Handles.COMBtimeSheetFormat,'Value'));
            % time=datestr(time,timeFormat);
            % time=mat2cell(time,ones(1,size(time,1)),size(time,2));
        end
    end
    if isempty(time)
        % errordlg('Invalid time point(s) exist in your current selection.  To reselect it, go back to Step 2.','Time Series Tools');
        flag=false;
        delete(h.Handles.bar);
        return;
    end
    % time pretreatment
    if timeFormat>=0
        if timeFormat==13 || timeFormat==14 || timeFormat==15 || timeFormat==16
            [year,month,day,hour,minute,second]=datevec(time);
            time=hour*3600+minute*60+second;
        end
    end
elseif ind==2
    % manual time vector
    SelectedRows=h.IOData.SelectedRows;
    SelectedColumns=h.IOData.SelectedColumns;
    if get(h.Handles.COMBuseFormat,'Value')==1
        % absolute date/time format
        % check start time
        try
            MatlabFormat=h.IOData.formatcell.matlabFormatString{get(h.Handles.COMBtimeManualFormat,'Value')};
            startTime=datestr(datenum(get(h.Handles.EDTtimeManualStart,'String')),MatlabFormat);
        catch
            errordlg('The start time in the manual input needs to be an absolute date format supported by MATLAB.  Please go back to Step 2 to change the Start Time editbox.','Time Series Tools');
            flag=false;
            delete(h.Handles.bar);
            return
        end
        % check interval
        try
            interval=eval(get(h.Handles.EDTtimeManualInterval,'String'));
        catch
            errordlg('The time step in the manual input needs to be a double value.  Please go back to Step 2 to change the Time Step editbox.','Time Series Tools');
            delete(h.Handles.bar);
            flag=false;
            return
        end
        if isnan(interval)
            errordlg('The time step in the manual input needs to be a double value.  Please go back to Step 2 to change the Time Step editbox.','Time Series Tools');
            flag=false;
            delete(h.Handles.bar);
            return
        end
        % check number of sample
        samples=str2double(get(h.Handles.EDTtimeManualEnd,'String'));
        if samples==0 || isnan(samples)
            errordlg('The length of time series is zero.  Please go back to Step 2 and use mouse to highlight the desired data block.','Time Series Tools');
            flag=false;
            delete(h.Handles.bar);
            return
        end
        time=localGetAbsTimeArray(h,startTime,interval,samples,MatlabFormat);
        timeFormat=h.IOData.formatcell.matlabFormatIndex(get(h.Handles.COMBtimeManualFormat,'Value'));
    else
        % relative time
        % check start time
        startTime=str2double(get(h.Handles.EDTtimeManualStart,'String'));
        if isnan(startTime)
            errordlg('The start time in the manual input needs to be a double value.  Please go back to Step 2 to change the Start Time editbox.','Time Series Tools');
            flag=false;
            delete(h.Handles.bar);
            return
        end
        % check interval
        try
            interval=eval(get(h.Handles.EDTtimeManualInterval,'String'));
        catch
            errordlg('The time step in the manual input needs to be a double value.  Please go back to Step 2 to change the Time Step editbox.','Time Series Tools');
            delete(h.Handles.bar);
            flag=false;
            return
        end
        if isnan(interval)
            errordlg('The time step in the manual input needs to be a double value.  Please go back to Step 2 to change the Time Step editbox.','Time Series Tools');
            flag=false;
            delete(h.Handles.bar);
            return
        end
        % check number of sample
        samples=str2double(get(h.Handles.EDTtimeManualEnd,'String'));
        if samples==0 || isnan(samples)
            errordlg('The length of time series is zero.  Please go back to Step 2 and use mouse to highlight the desired data block.','Time Series Tools');
            flag=false;
            delete(h.Handles.bar);
            return
        end
        time=(startTime:interval:startTime+interval*(samples-1));
        timeFormat=-1;
    end
elseif ind==3
    % time vector from a workspace variable
    if ~isempty(h.IOData.timeFromWorkspace)
        timeValue=h.IOData.timeFromWorkspace;
        timeFormat=h.IOData.timeFormatFromWorkspace;
        time=timeValue;
        SelectedRows=h.IOData.SelectedRows;
        SelectedColumns=h.IOData.SelectedColumns;
        if get(h.Handles.COMBdataSample,'Value')==1
            if length(timeValue)~=length(h.IOData.SelectedRows)
                % a sample is a row
                errordlg('The length of data block is not compatible with the length of the time vector.','Time Series Tools');
                flag=false;
                delete(h.Handles.bar);
                return
            end
            [SelectedIndex,flag]=h.checkBlankTimePoint(timeValue,'column');
            if ~flag
                delete(h.Handles.bar);
                return;
            end
            SelectedRows=h.IOData.SelectedRows(SelectedIndex);
            time=timeValue(SelectedIndex);
            % SelectedColumns=h.IOData.SelectedColumns(find(h.IOData.SelectedColumns~=get(h.Handles.COMBtimeIndex,'Value')));            
        else
            if length(timeValue)~=length(h.IOData.SelectedColumns)
                % a sample is a row
                errordlg('The length of data block is not compatible with the length of the time vector.','Time Series Tools');
                flag=false;
                delete(h.Handles.bar);
                return
            end
            [SelectedIndex,flag]=h.checkBlankTimePoint(timeValue,'row');
            if ~flag
                delete(h.Handles.bar);
                return;
            end
            SelectedColumns=h.IOData.SelectedColumns(SelectedIndex);
            time=timeValue(SelectedIndex);
            % SelectedRows=h.IOData.SelectedRows(find(h.IOData.SelectedRows~=get(h.Handles.COMBtimeIndex,'Value')));            
        end            
        if isempty(time)
            errordlg('Invalid time point(s) exist in your current selection.  To reselect it, go back to Step 2.','Time Series Tools');
            flag=false;
            delete(h.Handles.bar);
            return;
        end
        % time pretreatment
        if timeFormat>=0
            if timeFormat==13 || timeFormat==14 || timeFormat==15 || timeFormat==16
                [year,month,day,hour,minute,second]=datevec(time);
                time=hour*3600+minute*60+second;
            end
        end
    else
        flag=false;
        delete(h.Handles.bar);
        return;
    end
end
        
waitbar(40/100,h.Handles.bar);

% -------------------------------------------------------------------------
% get data block
% -------------------------------------------------------------------------
if iscell(dataOriginal)
    try
        data=cell2mat(dataOriginal(SelectedRows,SelectedColumns));
    catch
        errordlg('Data must contain numeric values only.','Time Series Tools');
        flag=false;
        delete(h.Handles.bar);
        return
    end
    if ~isnumeric(data)
        errordlg('Data must contain numeric values only.','Time Series Tools');
        flag=false;
        delete(h.Handles.bar);
        return
    end
elseif isnumeric(dataOriginal)
    data=dataOriginal(SelectedRows,SelectedColumns);
else
    errordlg('Data must contain numeric values only.','Time Series Tools');
    flag=false;
    delete(h.Handles.bar);
    return
end
if isempty(data)
    errordlg('Selected data block is empty.  To reselect it, go back to Step 2.','Time Series Tools');
    flag=false;
    delete(h.Handles.bar);
    return
end

waitbar(70/100,h.Handles.bar);

% -------------------------------------------------------------------------
% sort based on time if it is relative time
% -------------------------------------------------------------------------
if ~iscell(time)
    [time sortindex]=sort(time);
    if get(h.Handles.COMBdataSample,'Value')==1
        % in the excel sheet, a sample is a row
        data=data(sortindex,:);
    else
        % in the excel sheet, a sample is a column
        data=data(:,sortindex);
    end
end

% -------------------------------------------------------------------------
% check duplicated time points
% -------------------------------------------------------------------------
[tmp1,tmp2,tmp3]=unique(time);
index=diff(tmp3);
if sum(index==0)>0
    % duplicated time points exist
    ButtonDuplicated=questdlg('The time vector contains duplicated time points.  For the samples related to the same time points, you can either select the last sample or the mean of the samples to import.', ...
        'Time Series Tools', 'Select the last', 'Select the average', 'Abort', 'Select the average');
    drawnow;
    switch ButtonDuplicated
        case 'Select the last'
            time=time([true;~(index==0)]);
            if get(h.Handles.COMBdataSample,'Value')==1
                % in the excel sheet, a sample is a row
                data=data([~(index==0);true],:);
            else
                % in the excel sheet, a sample is a column
                data=data(:,[~(index==0);true]);
            end
        case 'Select the average'
            time=time([true;~(index==0)]);
            if get(h.Handles.COMBdataSample,'Value')==1
                % in the excel sheet, a sample is a row
                k=1;
                for i=1:length(index)
                    if index(i)==0
                        data(i+1,:)=(data(i,:)*k+data(i+1,:))/(k+1);
                        k=k+1;
                    else
                        k=1;
                    end
                end
                data=data([~(index==0);true],:);
            else
                % in the excel sheet, a sample is a column
                k=1;
                for i=1:length(index)
                    if index(i)==0
                        data(:,i+1)=(data(:,i)*k+data(:,i+1))/(k+1);
                        k=k+1;
                    else
                        k=1;
                    end
                end
                data=data(:,[~(index==0);true]);
            end
        case 'Abort'
            flag=false;
            delete(h.Handles.bar);
            return
    end
end

waitbar(80/100,h.Handles.bar);

% -------------------------------------------------------------------------
% create/update time series object(s)
% -------------------------------------------------------------------------
if get(h.Parent.Handles.PNLoption,'SelectedObject')==h.Parent.Handles.RADIOsingleNEW
    % create timeseries object
    try
        if ~strcmp(ButtonSameName,'Replace')
            if get(h.Handles.COMBdataSample,'Value')==1
                % in the excel sheet, a sample is a row
                ts=tsdata.timeseries(data,time,UserInputName);
            else
                % in the excel sheet, a sample is a column
                ts=tsdata.timeseries(data',time,UserInputName);
            end
            if timeFormat>=0
                ts.timeInfo.Format=h.IOData.formatcell.matlabFormatString{find(h.IOData.formatcell.matlabFormatIndex == timeFormat)};
                ts.timeInfo.unit='sec';
            end
            ts.metadata.Data.GridFirst=1;
            tstool(ts);
        else
            tsgui = tstool;
            ts = tsgui.Tsnode.getChildren('Label',UserInputName).Timeseries;
            ts.timeinfo.Startdate='';
            ts.timeinfo.Format='';
            if get(h.Handles.COMBdataSample,'Value')==1
                % in the excel sheet, a sample is a row
                ts.init(data,time,UserInputName);
            else
                % in the excel sheet, a sample is a column
                ts.init(data',time,UserInputName);
            end
            if timeFormat>=0
                ts.timeInfo.Format=h.IOData.formatcell.matlabFormatString{find(h.IOData.formatcell.matlabFormatIndex == timeFormat)};
                ts.timeInfo.unit='sec';
            end
            ts.metadata.Data.GridFirst=1;
        end
    catch
        errordlg(sprintf('%s\n\n%s','Error in creating time series object in tstool :',lasterr),'Time Series Tools');
        flag=false;
        delete(h.Handles.bar);
        return
    end
elseif get(h.Parent.Handles.PNLoption,'SelectedObject')==h.Parent.Handles.RADIOsingleINSERT
    % get the timeseries object from the gui
    tsgui = tstool;
    ts = tsgui.Tsnode.getChildren('Label',UserInputName).Timeseries;
    % check if the two time vectors are the same: if they are the same
    % then insert data, if they are not the same size, abort the
    % operation, if they have the same size but not the same value, ask
    % user which time vector to choose
    try
        tsTime=ts.getAbsTime;
    catch
        tsTime=ts.time;
    end
    if length(tsTime)==length(time)
        % time vector has the same size, now compare values
        if isequal(tsTime,time)
            % same time vector
            ButtonName='Use the old time vector';
        else
            % two time vectors have different values
            ButtonName=questdlg('The new time vector you selected has a defferent length from the old time vector used in the existing timeseries object.', ...
                'Time Series Tools', 'Use the old time vector', 'Use the new time vector', 'Abort', 'Use the old time vector');
        end
    else
        % time vector has different sizes
        ButtonName=questdlg('The new time vector you selected is different from the old time vector used in the existing timeseries object.', ...
            'Time Series Tools', 'Replace', 'Abort', 'Replace');
    end
    drawnow;
    
    % create timeseries object
    switch ButtonName,
        case 'Use the old time vector',
            ;
        case 'Use the new time vector',
            tmpdata=ts.data;
            [time tmpIndex]=sort(time);
            if get(h.Handles.COMBdataSample,'Value')==2
                data=data(:,tmpIndex);
            else
                data=data(tmpIndex,:);
            end
            ts.timeinfo.Startdate='';
            ts.timeinfo.Format='';
            ts.init(tmpdata,time);
            if timeFormat>=0
                ts.timeInfo.Format=h.IOData.formatcell.matlabFormatString{find(h.IOData.formatcell.matlabFormatIndex == timeFormat)};
                ts.timeInfo.unit='sec';
            end
        case 'Replace',
            ts.timeinfo.Startdate='';
            ts.timeinfo.Format='';
            ts.init(data,time);
            if timeFormat>=0
                ts.timeInfo.Format=h.IOData.formatcell.matlabFormatString{find(h.IOData.formatcell.matlabFormatIndex == timeFormat)};
                ts.timeInfo.unit='sec';
            end
            flag=true;
            delete(h.Handles.bar);
            return
        case 'Abort',
            flag=false;
            delete(h.Handles.bar);
            return;
    end
    if ts.metadata.Data.Gridfirst==1
        % in the existing timeseries, a sample is a row
        if get(h.Handles.COMBdataSample,'Value')==2
            % in the excel sheet, a sample is a column
            ts.data=[ts.data data'];
        else
            % in the excel sheet, a sample is a row
            ts.data=[ts.data data];
        end
    else
        % in the existing timeseries, a sample is a column
        if get(h.Handles.COMBdataSample,'Value')==2
            % in the excel sheet, a sample is a column
            data=reshape(data,[size(data,1) 1 size(data,2)]);
            ts.data=cat(1,ts.data,data);
        else
            % in the excel sheet, a sample is a row
            data=data';
            data=reshape(data,[size(data,1) 1 size(data,2)]);
            ts.data=cat(1,ts.data,data);
        end
    end
else
    failed_count=0;
    % multiple timeseries
    if get(h.Handles.COMBdataSample,'Value')==2
        % a sample is a column
        for i=1:length(SelectedRows)
            % save timeseries
            try
                if get(h.Parent.Handles.COMBmultipleNEW,'Value')==1
                    % use a row or column as timeseries name
                    tmpName=dataOriginal(i,UserInputName);
                    if iscell(tmpName)
                        tmpName=cell2mat(tmpName);
                    end
                    if ~ischar(tmpName) || ~isvarname(tmpName)
                        failed_count=failed_count+1;
                        continue;
                    end
                else
                    tmpName=strcat(UserInputName,num2str(i));
                end
                ts=tsdata.timeseries(data(i,:),time,tmpName);
                if timeFormat>=0
                    ts.timeInfo.Format=h.IOData.formatcell.matlabFormatString{find(h.IOData.formatcell.matlabFormatIndex == timeFormat)};
                    ts.timeInfo.unit='sec';
                end
                if get(h.Handles.COMBdataSample,'Value')==2
                    % a sample is a column, use gridfirst=false
                    ts.metadata.Data.GridFirst=0;
                else
                    % a sample is a row, use gridfirst=true
                    ts.metadata.Data.GridFirst=1;
                end
                tstool(ts);
            catch
                errordlg(sprintf('%s\n\n%s','Error in creating time series object in tstool :',lasterr),'Time Series Tools');
                flag=false;
                delete(h.Handles.bar);
                return
            end
        end
    else
        % a sample is a row
        for j=1:length(SelectedColumns)
            % for each measurement
            % get data
            try
                if get(h.Parent.Handles.COMBmultipleNEW,'Value')==1
                    % use a row or column as timeseries name
                    tmpName=dataOriginal(UserInputName,j);
                    if iscell(tmpName)
                        tmpName=cell2mat(tmpName);
                    end
                    if ~ischar(tmpName) || ~isvarname(tmpName)
                        failed_count=failed_count+1;
                        continue;
                    end
                else
                    % use the given name + suffix as the timeseries name
                    tmpName=strcat(UserInputName,num2str(j));
                end
                ts=tsdata.timeseries(data(:,j),time,tmpName);
                if timeFormat>=0
                    ts.timeInfo.Format=h.IOData.formatcell.matlabFormatString{find(h.IOData.formatcell.matlabFormatIndex == timeFormat)};
                    ts.timeInfo.unit='sec';
                end
                if get(h.Handles.COMBdataSample,'Value')==2
                    % a sample is a column, use gridfirst=false
                    ts.metadata.Data.GridFirst=0;
                else
                    % a sample is a row, use gridfirst=true
                    ts.metadata.Data.GridFirst=1;
                end
                tstool(ts);
            catch
                errordlg(sprintf('%s\n\n%s','Error in creating time series object in tstool :',lasterr),'Time Series Tools');
                flag=false;
                delete(h.Handles.bar);
                return
            end
        end
    end
    if failed_count>0
        msgbox('Some time series objects are not imported due to the presence of invalid names','Time Series Tools');
    end
end

waitbar(100/100,h.Handles.bar);

%populate combobox with current timeseries objects in the base workspace
tsgui = tstool;
alltsobjects = get(tsgui.Tsnode.getChildren,{'Timeseries'});
if length(alltsobjects)>0
    strCell={};
    for i=1:length(alltsobjects)
        strCell=[strCell;{alltsobjects{i}.Name}];
    end
else
    strCell={''};
end
set(h.Parent.Handles.COMBsingleINSERT,'String',strCell,'Value',1);
flag=true;

delete(h.Handles.bar);

function array=localGetAbsTimeArray(h,startTime,interval,samples,MatlabFormat)
% format 4, 5, 6, 
startValue=datevec(startTime,MatlabFormat);
switch h.IOData.formatcell.matlabFormatIndex(get(h.Handles.COMBtimeManualFormat,'Value'))
    case {0 13 14 21} 
        %'dd-mmm-yyyy HH:MM:SS' 'HH:MM:SS' 'HH:MM:SS PM' 'mmm.dd,yyyy HH:MM:SS'
        endValue=startValue;
        endValue(6)=endValue(6)+interval*(samples-1);
        array=datestr(datenum(startValue):interval/86400:datenum(endValue));
        array=mat2cell(array,ones(1,size(array,1)),size(array,2));
    case {1 2 6 22 23}
        endValue=startValue;
        endValue(3)=endValue(3)+interval*(samples-1);
        array=datestr(datenum(startValue):interval:datenum(endValue));
        array=mat2cell(array,ones(1,size(array,1)),size(array,2));
    case {15 16}
        endValue=startValue;
        endValue(5)=endValue(5)+interval*(samples-1);
        array=datestr(datenum(startValue):interval/1440:datenum(endValue));
        array=mat2cell(array,ones(1,size(array,1)),size(array,2));
end

