ures;
    try
        mcodeoutput = evalc('evalin(''base'',thisCell.code)');
    catch mExc
        mcodeoutput = '';
        disp(formatError(mExc))
        beep
        cellFilename = '';
    end
    changedFigures = internal.matlab.publish.compareFigures(figureState);
    if ~isempty(mcodeoutput)
        fprintf('%s\n',mcodeoutput);
    end
    for i = 1:length(changedFigures)
        figure(changedFigures(i))
    end
end

lastCell = cellIndex;
lastCellFinished = now;

function m = formatError(laste)
m = getReport(laste);

% Trim out the ECHODEMO stack from the error.
iRet = find(m==sprintf('\n'));
instrumentAndRunPos = strfind(m,'echodemo');
stackStart = max(iRet(iRet < instrumentAndRunPos(end)));
causePos = strfind(m,getCauseString);
if isempty(causePos)
    stackEnd = numel(m);
else
    stackEnd = max(iRet(iRet < causePos(end)))-1;
end
m(stackStart:stackEnd) = [];

% Trim trailing newline.
if ~isempty(causePos)
    m(end) = [];
end

function s = getCauseString
M1 = MException('foo:bar','');
M2 = MException('foo2:bar2','');
M2 = M2.addCause(M1);
s = strtrim(getReport(M2));

