Advanced Topics
UnderstandingPythonand马铃薯草进口
Commands
The进口
语句在matlab中没有相同的功能®as in Python®。
LoadPythonModule in马铃薯草
Pythoncode uses the进口
语句加载和制作代码可访问。MATLAB在键入时自动加载Pythonpy。
在模块名称和函数名称前面。此代码显示如何调用函数wrap
in Python module文本换行
。
PythonCode | MATLAB代码 |
---|---|
进口文本换行pS1 = textwrap.wrap('This is a string') |
s1 = py.textwrap.wrap('This is a string');
|
警告
In MATLAB, do not type:
进口Pythonmodule.
Never call:
进口py。*
If you do, then MATLAB calls the Python function instead of the MATLAB function of the same name. This can cause unexpected behavior. If you type this进口
command, then you must call the MATLAB command:
clear import
Shorten Class or Function Names
The Pythonfrom...import
声明允许您在不使用完全限定的名称的情况下引用模块。在Matlab中,使用进口
功能。This code shows how to reference functionwrap
in Python module文本换行
。Sincewrap
is not a MATLAB function, you can shorten the calling syntax using the进口
功能。After calling this command, you do not need to type the package (py
) and module (文本换行
) names.
PythonCode | MATLAB代码 |
---|---|
进口文本换行pS1 = textwrap.wrap('This is a string') from textwrap import wrap pS2 = wrap('another string') |
s1 = py.textwrap.wrap('This is a string'); importpy.textwrap.wrap.S2 =包裹('another string'); |
进口mymod.as mm |
mm = py.importlib.import_module('mymod.'); % Use mm as an alias to access functionality inmymod. |
帮助PythonFunctions
有关Python功能的完整描述,特别是咨询外部资源,特别是https://www.python.org。Python文档有不同的版本,因此请务必引用与系统上的版本对应的版本。MATLAB文档中的许多示例在Python标准库中引用了函数。
To use functions in a third-party or user-defined Python module, refer to your vendor product documentation for information about how to install the module and for details about its functionality.
The MATLABpy。help
command displays the Python help found atwww.python.org./doc
。软件包和类的帮助可以广泛,在MATLAB命令窗口中显示时可能无效。
包裹
py.help('textwrap')
班级
py.help('textwrap.textwrapper')
班级方法
py.help('textwrap.textwrapper.wrap')
Function
py.help('textwrap.fill')
如果MATLAB从开头显示错误消息PythonError:
,请参阅您的Python文档以获取更多信息。
Note
选项卡完成不显示可用的Python功能。
您不能使用交互式Python帮助 - 调用py。help
没有输入参数 - 在matlab中。
CallPython方法马铃薯草名称冲突
If a Python method name is the name of a sealed method of a MATLAB base class or reserved function, then MATLAB renames the method. The new name starts with the letterx
and changes the first letter of the original name to uppercase. For example, MATLAB renames the Python method猫
toxCat
。有关保留方法列表,请参阅修改默认行为的方法。
If a method name is a MATLAB keyword, then MATLAB callsmatlab.lang.makeValidName
to rename the method. For a list of keywords, seeiskeyword
。
If a generated name is a duplicate name, then MATLAB renames the method usingmatlab.lang.makeunqueStrings.
。
CallPythoneval
Function
This example shows how to evaluate the expressionx+y
使用python.eval
命令。readeval
。
py.help('eval')
帮助模块中内置函数eval __builtin__:eval(...)eval(source [,globals [,locals]) - >值在全局和当地人的上下文中评估源。源可以是表示Python表达式的字符串或由Compile()返回的代码对象。全局必须是字典,当地人可以是任何映射,默认到当前的全局和当地人。如果仅给出全局,则当地人默认为此。
要评估表达式,请传递Pythond
价值globals
namespace parameter.
创建一个python.d
可变的x
andy
values.
workspace = py.dict(Pyargs('x',1,'y',6))
workspace = Python dict with no properties. {'y': 6.0, 'x': 1.0}
Evaluate the expression.
res = py.eval('x + y',工作区)
res = 7
Alternatively, to add two numbers without assigning variables, pass an emptyd
价值globals
范围。
res = py.eval('1+6',py.dict)
res = 7
Execute CallablePython目的
要执行可调用的Python对象,请使用Feval.
功能。例如,如果实例obj
of a Python class is callable, replace the Python syntaxobj(x1, ..., xn)
与以下一个Matlab陈述之一:
Feval.(obj,x1,...,xn)
obj(x1,...,xn)
How马铃薯草RepresentsPython运营商
马铃薯草supports the following overloaded operators.
Python运算符符号 | Python方法s | MATLAB方法 |
---|---|---|
+ (二进制) |
__add__ ,__radd__ |
plus ,+ |
- (二进制) |
__sub__ ,__sub__ |
minus ,- |
* (二进制) |
__mul__ ,__rmul__ |
mtimes ,* |
/ |
__Trudiv__ ,__rtruediv__ |
mrdivide ,/ |
==. |
__eq__ |
eq ,==. |
> |
__gt__ |
gt ,> |
< |
__lt__ |
lt ,< |
!= |
__ne__ |
ne ,〜= |
> = |
__ge__ |
ge ,> = |
<= |
__le__ |
le ,<= |
- (联合律) |
__neg__ |
uminus ,-a |
+ (联合律) |
__pos__ |
uplus ,+a |
The following Python operators are not supported.
Python运算符符号 | Python方法 |
---|---|
% |
__mod__ ,__rmod__ |
** |
__pow__ ,__rpow__ |
<< |
__lshift__ ,__rlshift__ |
>> |
__rshift__ ,__rrshift__ |
& |
__and__ ,__rand__ |
^ |
__xor__ ,__rxor__ |
| |
__要么__ ,__ror__ |
// (二进制) |
__floordiv__ ,__rfloordiv__ |
+= (联合律) |
__我加__ |
-= (联合律) |
__isub__ |
*= (联合律) |
__imul__ |
/= (联合律) |
__itrudiv__ |
//= (联合律) |
__ifloordiv__ |
%= (联合律) |
__imod__ |
** = (联合律) |
__ipow__ |
<<= (联合律) |
__ilshift__ |
>>= (联合律) |
__Irshift__ |
&= (联合律) |
__我和__ |
^ = (联合律) |
__ixor__ |
!= (联合律) |
__ior__ |
~ (联合律) |
__invert__ |