Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

高级主题

了解 PythonMATLAB import 命令

import 语句在 MATLAB® 中的功能与在 Python® 中的功能不同。

MATLAB 中加载 Python 模块

Python 代码使用 import 语句来加载代码并使其可访问。当您在模块名称和函数名称前面键入 py. 时,MATLAB 会自动加载 Python。以下代码显示如何在 Python 模块 textwrap 中调用函数 wrap

Python 代码MATLAB 代码
import textwrap
pS1 = textwrap.wrap('This is a string')
S1 = py.textwrap.wrap('This is a string');

小心

在 MATLAB 中,请勿键入:

import pythonmodule

切勿调用:

import py.*

如果执行了此调用,则 MATLAB 会调用 Python 函数,而不是同名的 MATLAB 函数。这可能会导致意外的行为。如果您使用了 import 命令,必须记得调用以下 MATLAB 命令来取消它:

clear import

缩短类或函数名称

借助 Python from...import 语句,您可以在不使用完全限定名称的情况下引用模块。在 MATLAB 中,使用 import 函数。以下代码显示如何在 Python 模块 textwrap 中引用函数 wrap。由于 wrap 不是 MATLAB 函数,您可以使用 import 函数缩短调用语法。调用此命令后,您不需要键入包 (py) 和模块 (textwrap) 名称。

Python 代码MATLAB 代码
import textwrap
pS1 = textwrap.wrap('This is a string')
from textwrap import wrap
pS2 = wrap('another string')
S1 = py.textwrap.wrap('This is a string');
import py.textwrap.wrap
S2 = wrap('another string');
import mymod as mm
mm = py.importlib.import_module('mymod');
% Use mm as an alias 
% to access functionality 
% in mymod

Python 函数的帮助

有关 Python 功能的完整说明,请咨询外部资源,尤其是 https://www.python.org。Python 文档有不同版本,请务必参考与您系统上的版本对应的文档版本。MATLAB 文档中的许多示例引用 Python 标准库中的函数。

要使用第三方或用户定义的 Python 模块中的函数,请参阅您的供应商产品文档,了解有关如何安装该模块的信息以及有关其功能的详细信息。

MATLAB py.help 命令显示 www.python.org/doc 上提供的 Python 帮助。包和类的帮助可能包含很多内容,在 MATLAB 命令行窗口中显示时可能不是很有用。

  • py.help('textwrap')
  • py.help('textwrap.TextWrapper')
  • 类的方法

    py.help('textwrap.TextWrapper.wrap')
  • 函数

    py.help('textwrap.fill')

如果 MATLAB 显示以 Python Error: 开头的错误消息,请参考您的 Python 文档以了解详细信息。

注意

Tab 键自动填充不显示可用的 Python 功能。

您无法在 MATLAB 中使用交互式 Python 帮助 - 调用不带输入参数的 py.help

使用 MATLAB 调用 Python 方法时发生名称冲突

如果 Python 方法名称是 MATLAB 基类或保留函数的 Sealed 方法的名称,则 MATLAB 会重命名该方法。新名称以字母 x 开头,并将原始名称的第一个字母改为大写。例如,MATLAB 将 Python 方法 cat 重命名为 xCat。有关保留方法的列表,请参阅Methods That Modify Default Behavior

如果方法名称是 MATLAB 关键字,则 MATLAB 调用 matlab.lang.makeValidName 来重命名该方法。有关关键字列表,请参阅 iskeyword

如果生成的名称是重复名称,则 MATLAB 使用 matlab.lang.makeUniqueStrings 重命名该方法。

调用 Python eval 函数

此示例说明如何使用 Python eval 命令计算表达式 x+y。阅读 eval 的帮助。

py.help('eval')
Help on built-in function eval in module __builtin__:

eval(...)
    eval(source[, globals[, locals]]) -> value
    
    Evaluate the source in the context of globals and locals.
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.

要计算表达式,请为 globals 命名空间参数传递一个 Python dict 值。

xy 值创建一个 Python dict 变量。

workspace = py.dict(pyargs(x=1,y=6))
workspace = 
  Python dict with no properties.

    {'y': 6.0, 'x': 1.0}

计算该表达式。

res = py.eval('x+y',workspace)
res = 7

或者,要添加两个数值而不分配变量,请为 globals 参数传递一个空的 dict 值。

res = py.eval('1+6',py.dict)
res = 

  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    7

执行可调用的 Python 对象

要执行可调用的 Python 对象,请使用 feval 函数。例如,如果 Python 类的实例 obj 是可调用的,请用以下 MATLAB 语句之一替换 Python 语法 obj(x1, ..., xn)

feval(obj,x1, ..., xn)
obj(x1, ..., xn)

MATLAB 如何表示 Python 运算符

MATLAB 支持以下重载运算符。

Python 运算符符号Python 方法MATLAB 方法
+(二元) __add__, __radd__plus, +
-(二元) __sub__, __rsub__ minus, -
*(二元) __mul__, __rmul__mtimes, *
/__truediv__, __rtruediv__mrdivide, /
==__eq__eq, ==
>__gt__gt, >
<__lt__lt, <
!=__ne__ne, ~=
>=__ge__ge, >=
<=__le__le, <=
-(一元)__neg__uminus, -a
+(一元)__pos__uplus, +a

不支持以下 Python 运算符。

Python 运算符符号Python 方法
%__mod__, __rmod__
**__pow__, __rpow__
<<__lshift__, __rlshift__
>>__rshift__, __rrshift__
&__and__, __rand__
^__xor__, __rxor__
|__or__, __ror__
//(二元) __floordiv__, __rfloordiv__
+=(一元) __iadd__
-=(一元) __isub__
*=(一元) __imul__
/=(一元) __itruediv__
//=(一元) __ifloordiv__
%=(一元) __imod__
**=(一元) __ipow__
<<=(一元) __ilshift__
>>=(一元) __irshift__
&=(一元) __iand__
^=(一元) __ixor__
|=(一元) __ior__
~(一元) __invert__

另请参阅

|

相关主题

外部网站