当前位置: 首页>>代码示例>>Python>>正文


Python Matlab.eval方法代码示例

本文整理汇总了Python中pymatbridge.Matlab.eval方法的典型用法代码示例。如果您正苦于以下问题:Python Matlab.eval方法的具体用法?Python Matlab.eval怎么用?Python Matlab.eval使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pymatbridge.Matlab的用法示例。


在下文中一共展示了Matlab.eval方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MatlabEngine

# 需要导入模块: from pymatbridge import Matlab [as 别名]
# 或者: from pymatbridge.Matlab import eval [as 别名]
class MatlabEngine(object):

    def __init__(self):
        if 'OCTAVE_EXECUTABLE' in os.environ:
            self._engine = Octave(os.environ['OCTAVE_EXECUTABLE'])
            self._engine.start()
            self.name = 'octave'
        elif matlab_native:
            self._engine = matlab.engine.start_matlab()
            self.name = 'matlab'
        else:
            executable = os.environ.get('MATLAB_EXECUTABLE', 'matlab')
            self._engine = Matlab(executable)
            self._engine.start()
            self.name = 'pymatbridge'
        # add MATLAB-side helper functions to MATLAB's path
        if self.name != 'octave':
            kernel_path = os.path.dirname(os.path.realpath(__file__))
            toolbox_path = os.path.join(kernel_path, 'toolbox')
            self.run_code("addpath('%s');" % toolbox_path)

    def run_code(self, code):
        if matlab_native:
            return self._run_native(code)
        return self._engine.run_code(code)

    def stop(self):
        if matlab_native:
            self._engine.exit()
        else:
            self._engine.stop()

    def _run_native(self, code):
        resp = dict(success=True, content=dict())
        out = StringIO()
        err = StringIO()
        if sys.version_info[0] < 3:
            code = str(code)
        try:
            self._engine.eval(code, nargout=0, stdout=out, stderr=err)
            self._engine.eval('''
                figures = {};
                handles = get(0, 'children');
                for hi = 1:length(handles)
                    datadir = fullfile(tempdir(), 'MatlabData');
                    if ~exist(datadir, 'dir'); mkdir(datadir); end
                    figures{hi} = [fullfile(datadir, ['MatlabFig', sprintf('%03d', hi)]), '.png'];
                    saveas(handles(hi), figures{hi});
                    if (strcmp(get(handles(hi), 'visible'), 'off')); close(handles(hi)); end
                end''', nargout=0, stdout=out, stderr=err)
            figures = self._engine.workspace['figures']
        except (SyntaxError, MatlabExecutionError) as exc:
            resp['content']['stdout'] = exc.args[0]
            resp['success'] = False
        else:
            resp['content']['stdout'] = out.getvalue()
            if figures:
                resp['content']['figures'] = figures
        return resp
开发者ID:bernardosabatinilab,项目名称:VC-FlowIn-Analysis-Banghart-Neufeld-2015,代码行数:61,代码来源:kernel.py

示例2: MatlabEngine

# 需要导入模块: from pymatbridge import Matlab [as 别名]
# 或者: from pymatbridge.Matlab import eval [as 别名]
class MatlabEngine(object):

    def __init__(self):
        if 'OCTAVE_EXECUTABLE' in os.environ:
            self._engine = Octave(os.environ['OCTAVE_EXECUTABLE'])
            self._engine.start()
            self.name = 'octave'
        elif matlab_native:
            self._engine = matlab.engine.start_matlab()
            self.name = 'matlab'
        else:
            executable = os.environ.get('MATLAB_EXECUTABLE', 'matlab')
            self._engine = Matlab(executable)
            self._engine.start()
            self.name = 'pymatbridge'
        # add MATLAB-side helper functions to MATLAB's path
        if self.name != 'octave':
            kernel_path = os.path.dirname(os.path.realpath(__file__))
            toolbox_path = os.path.join(kernel_path, 'toolbox')
            self.run_code("addpath('%s');" % toolbox_path)

    def run_code(self, code):
        if matlab_native:
            return self._run_native(code)
        return self._engine.run_code(code)

    def stop(self):
        if matlab_native:
            self._engine.exit()
        else:
            self._engine.stop()

    def _run_native(self, code):
        out = StringIO()
        err = StringIO()
        if sys.version_info[0] < 3:
            code = str(code)
        try:
            self._engine.eval(code, nargout=0, stdout=out, stderr=err)
        except (SyntaxError, MatlabExecutionError) as exc:
            return dict(success=False, content=dict(stdout=exc.args[0]))
        return dict(success=True, content=dict(stdout=out.getvalue()))
开发者ID:benjamin-heasly,项目名称:matlab_kernel,代码行数:44,代码来源:kernel.py


注:本文中的pymatbridge.Matlab.eval方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。