function success = eval(h)

success = false;

%% Get recorder/transaction handle
recorder = tsguis.recorder;
T = tsguis.transaction;

%% Find the selected time series
tableData = cell(h.Handles.tsTable.Data);
if isempty(tableData) % No timeseries in the table
    return
end
I = ~cellfun('isempty',tableData(:,2)) & cell2mat(tableData(:,1));
tsSelected = tableData(I,2);
if isempty(tsSelected)
    return
end

%% If logging write the header for this subsection
if strcmp(recorder.Recording,'on')
   T.addbuffer('%% Time series resample/merge');
end 

%% Resample using a vector from another time series
if get(h.Handles.RADIOtimeseries,'Value') 
    % Get time series which provides the time vector
    tsComboList = get(h.Handles.COMBts,'String');
    selectedTimeseriesName = tsComboList{get(h.Handles.COMBts,'Value')};
    thists = h.ViewNode.Plot.getTimeSeries(selectedTimeseriesName);
    
    % Get its time vector
    time = thists.Time;  
    if strcmp(recorder.Recording,'on')
       T.addbuffer(sprintf('time = %s.Time;',thists.Name),thists);
    end      
    
    % Resample the other selelected time series on this time vector
    if length(time)>1 % resample inteprets sclar time vectors as intervals
        for k=1:length(tsSelected)
            tsin = h.ViewNode.Plot.getTimeSeries(tsSelected{k});
            if get(h.Handles.RADIOinplace,'Value') % In place resample
                tsin.resample(time);
                if strcmp(recorder.Recording,'on')
                   T.addbuffer(sprintf('%s.resample(time);',tsin.Name),tsin);
                end        
            else % Resample to a copy
                tstemp = tsin.copy;
                tstemp.resample(time);
                tstemp.Name = sprintf('Resampled_%s',tsin.Name);
                %h.Parentviewnode.getRoot.tsViewer.TSnode.createChild(tstemp);
                h.viewnode.getRoot.tsViewer.TSnode.createChild(tstemp);
                if strcmp(recorder.Recording,'on')
                   T.addbuffer(sprintf('Resampled_%s = %s.copy;',tsin.Name,tsin.Name));               
                   T.addbuffer(sprintf('Resampled_%s.resample(time);',tsin.Name),tsin,tstemp);          
                end 
            end
        end
    end
%% resample using a uniform time vector    
elseif get(h.Handles.RADIOuniform,'Value') 
    % Find the units
    unitlist = get(h.Handles.COMBunits,'String');
    thisunit = unitlist{get(h.Handles.COMBunits,'Value')};
    
    % Get the time interval
    try
        interval = eval(get(h.Handles.EDITinterval,'String'));
    catch
        errordlg('Invalid interval','Time Series Tools','modal')
        return
    end
    if isempty(interval) || ~isscalar(interval)
        errordlg('Invalid interval','Time Series Tools','modal')
        return
    end
        
    for k=1:length(tsSelected)
        % Get the uniform time vector for each selected time series
        tsin = h.ViewNode.Plot.getTimeSeries(tsSelected{k});
        
        % Convert interval units
        thisinterval = interval*tsunitconv(tsin.timeInfo.Units,thisunit);
        
        % New time vector
        time = tsin.timeInfo.Start:thisinterval:tsin.timeInfo.End;
        if length(time)>1 % resample inteprets sclar time vectors as intervals       
            if strcmp(recorder.Recording,'on')
               T.addbuffer(sprintf('time = %f:%f:%f;',tsin.timeInfo.Start,...
                   thisinterval,tsin.timeInfo.End));
            end 

            % Resample the timeseries
            if get(h.Handles.RADIOinplace,'Value') % In place resample
                 resample(tsin,time);
                 if strcmp(recorder.Recording,'on')
                     T.addbuffer(sprintf('%s.resample(time);',tsin.Name),tsin);
                 end 
            else % Resample a copy
                tstemp = tsin.copy;
                tstemp.resample(time);
                tstemp.Name = sprintf('Resampled_%s',tsin.Name);
                h.viewnode.getRoot.tsViewer.TSnode.createChild(tstemp);
                %h.Parentviewnode.getRoot.tsViewer.TSnode.createChild(tstemp);
                if strcmp(recorder.Recording,'on')
                   T.addbuffer(sprintf('Resampled_%s = %s.copy;',tsin.Name,tsin.Name));               
                   T.addbuffer(sprintf('Resampled_%s.resample(time);',tsin.Name),tsin,tstemp);          
                end  
            end 
        else
            errordlg('Mergeing or resampling cannot produce time series with one sample or less',...
                'Time Series Tool','modal')
            return
        end
    end
%% Merge two timeseries
elseif get(h.Handles.RADIOunion,'Value') || ...
        get(h.Handles.RADIOintersect,'Value')
    
    % Currently limited to two time series
    if length(tsSelected)>2
           errordlg('Merge is currently limited to two timeseries only',...
                'Time Series Tools','modal')
            return
    end
    
    % Find the method string and check that the common time vector will not
    % be empty. This must be done before the merge so that the operation is
    % not partially complete when any error occurs
    if get(h.Handles.RADIOunion,'Value')
        method = 'union';
        maxStart = -inf;
        minEnd = inf;
        for k=1:length(tsSelected)
            maxStart = max(h.ViewNode.Plot.getTimeSeries(tsSelected{k}).TimeInfo.Start, ...
                maxStart);
            minEnd = min(h.ViewNode.Plot.getTimeSeries(tsSelected{k}).TimeInfo.End, ...
                minEnd);
        end
        if maxStart>minEnd
            errordlg('Merged time vector is empty, operation aborted',...
                'Time Series Tools','modal')
            return
        end
    elseif get(h.Handles.RADIOintersect,'Value')
        method = 'intersection';
        commontimes = h.ViewNode.Plot.getTimeSeries(tsSelected{1}).Time;
        for k=2:length(tsSelected)
            commontimes = intersect(commontimes,h.ViewNode.Plot.getTimeSeries(tsSelected{k}).Time);
        end
        if isempty(commontimes)
            errordlg('Merged time vector is empty, operation aborted',...
                'Time Series Tools','modal')
            return
        end
    end
        
    % Find the common time vector
    ts1 = h.ViewNode.Plot.getTimeSeries(tsSelected{1});
    ts2 = h.ViewNode.Plot.getTimeSeries(tsSelected{2});
    if get(h.Handles.RADIOinplace,'Value') % In place resample
        merge(ts1,ts2,method);
        if strcmp(recorder.Recording,'on')
            T.addbuffer(sprintf('merge(%s,%s,''%s'');',ts1.Name,ts2.Name,method),{ts1,ts2});
        end 
    else % Create copies
        ts1temp = ts1.copy;
        ts2temp = ts2.copy;
        merge(ts1temp,ts2temp,method);
        ts1temp.Name = sprintf('Merged_%s',ts1temp.Name);
        ts2temp.Name = sprintf('Merged_%s',ts2temp.Name);
        h.viewnode.getRoot.tsViewer.TSnode.createChild(ts1temp);
        h.viewnode.getRoot.tsViewer.TSnode.createChild(ts2temp);
        if strcmp(recorder.Recording,'on')
             T.addbuffer(sprintf('Merged_%s = %s.copy;',ts1.Name,ts1.Name)); 
             T.addbuffer(sprintf('Merged_%s = %s.copy;',ts2.Name,ts2.Name));
             T.addbuffer(sprintf('merge(Merged_%s,Merged_%s,''%s'');', ...
                 ts1.Name,ts2.Name,method),{ts1, ts2},{ts1temp,ts2temp});          
        end 
    end
end

%% Store transaction
T.commit;
recorder.pushundo(T);

%% Resport success
success = true;
