function success = eval(h)

%% Apply button callback
success = false;

%% Get the expression
expression = get(h.Handles.TXTexp,'String');

%% Create transaction and get the handle to the event recorder
T = tsguis.transaction;
recorder = tsguis.recorder;

%% Evaluate it
tslist = get(h.SrcNode.getChildren,{'Timeseries'});
wzerosstatus = getfield(warning('query','MATLAB:divideByZero'),'state');
warning('off','MATLAB:divideByZero')
try
    [outval,tsind] = xeval(sprintf('%s;',expression),tslist);
catch
    msg = sprintf('%s: %s','Cannot evaluate expression, error returned was',...
        lasterr);
    errordlg(msg,'Time Series Tools','modal')
    return
end
warning(wzerosstatus,'MATLAB:divideByZero')

if isempty(outval)
    msgbox('Expression returned no output','Time Series Tools','modal')
    return 
end

if ~isa(outval,'tsdata.timeseries')
    if isscalar(outval)
       msg = sprintf('%s: %f','Scalar value was returned to the workspace as variable ans with value',...
           outval);
    elseif length(outval)>1
       msg = sprintf('%s: %s','Matrix value was returned to the workspace as variable ans with size',...
           num2str(size(outval)),'. Matrix has been displayed in the workspace.');
       disp(outval);
    else
       msg = 'Expression returned no output';
    end
    msgbox(msg,'Time Series Tools','modal')
    return
end     
        
%% If a time series is returned from the expression add it to the tree
newName = deblank(get(h.Handles.TXToutname,'String'));
if ~isempty(newName)
    outval.Name = newName;
    h.Srcnode.createChild(outval);
else
    errordlg('Output time series name(s) must be defined',...
        'Time Series Tool','modal')
    return
end

%% If the recorder is on, cache the M code in the transaction buffer
if strcmp(recorder.Recording,'on')
    tslist = tslist(tsind);
    
    % Create a string respresenting the first arg of xeval
    tslststr = sprintf('{%s',tslist{1}.Name);
    for k=2:length(tsind)
        tslststr = [tslststr, sprintf(',%s',tslist{k}.Name)];
    end
    tslststr = [tslststr, '}'];
    
    T.addbuffer('%% Application of MATLAB function/arithmetic');
    if ~isempty(outval)
        T.addbuffer(sprintf('%s = xeval(%s%s%s,%s);',outval.Name,'''',...
            expression,'''',tslststr),tslist,outval);
    else
        T.addbuffer(sprintf('%s = xeval(%s%s%s,%s);',outval.Name,'''',...
            expression,'''',tslststr),tslist);  
        %T.addbuffer(expression,tslist(tsind));
    end
end

%% Store transaction
T.Warnstring = 'You are attempting to undo an arithmetic operation which created a time series';
T.commit;
recorder.pushundo(T);

%% Report success
success = true;


