def eval(self,
                     expression,
                     identify_errors=True,
                     print_expression=True,
                     on_new_output=sys.stdout.write):
        """ Evaluates a matlab expression synchronously.

        If identify_erros is true, and the last output line after evaluating the
        expressions begins with '???' and excpetion is thrown with the matlab error
        following the '???'.
        If on_new_output is not None, it will be called whenever a new output is
        encountered. The default value prints the new output to the screen.
        The return value of the function is the matlab output following the call.
        """
        self._check_open()
        if print_expression:
            print expression
        self.process.stdin.write(expression)
        self.process.stdin.write('\n')
        ret = self._sync_output(on_new_output)
        # TODO(dani): Use stderr to identify errors.
        if identify_errors and ret.rfind('???') != -1:
            begin = ret.rfind('???') + 4
            end = ret.find('\n', begin)
            raise MatlabError(ret[begin:end])
        return ret