MATLAB 中如何创建一个 Python 对象

创建一个 Python 对象 pyObj 的语法为:

pyObj = py.modulename.ClassName(varargin)

 

varargin 是类名 ClassName 的 __init__ 指定的构造函数参数列表。

MATLAB 中,Python 对象是引用类型(句柄对象),不遵守 MATLAB 的赋值复制和按值传递规则。 复制句柄对象时,仅复制句柄,旧句柄和新句柄都引用相同的数据。 复制 MATLAB 变量(值对象)时,也会复制变量数据。 新变量与原始变量的更改无关。

在 Python 标准库中存在 textwrap 模块,以下语句将创建一个 TextWrapper 类:

tw = py.textwrap.TextWrapper

结果:

tw = 
  Python TextWrapper - 属性:

               max_lines: [1×1 py.NoneType]
         drop_whitespace: 1
       subsequent_indent: [1×0 py.str]
             placeholder: [1×6 py.str]
                 tabsize: [1×1 py.int]
             expand_tabs: 1
        break_long_words: 1
        break_on_hyphens: 1
          initial_indent: [1×0 py.str]
    fix_sentence_endings: 0
                   width: [1×1 py.int]
      replace_whitespace: 1
    <textwrap.TextWrapper object at 0x0000000007271048>

创建默认的 TextWrapper 对象。 语句中不存在输入参数,因为每个参数都有一个默认值,由等号(=)字符标识。可以通过该类的  __init__ 方法查看默认参数的值:

py.help('textwrap.TextWrapper.__init__')

结果:

Help on function __init__ in textwrap.TextWrapper:

textwrap.TextWrapper.__init__ = __init__(self, width=70, initial_indent='', subsequent_indent='', expand_tabs=True, replace_whitespace=True, fix_sentence_endings=False, break_long_words=True, drop_whitespace=True, break_on_hyphens=True, tabsize=8, *, max_lines=None, placeholder=' [...]')
    Initialize self.  See help(type(self)) for accurate signature.

如果对象属性是逻辑值,可以直接赋值改变:

tw.break_long_words = 0

结果:

tw = 
  Python TextWrapper - 属性:

               max_lines: [1×1 py.NoneType]
         drop_whitespace: 1
       subsequent_indent: [1×0 py.str]
             placeholder: [1×6 py.str]
                 tabsize: [1×1 py.int]
             expand_tabs: 1
        break_long_words: 0
        break_on_hyphens: 1
          initial_indent: [1×0 py.str]
    fix_sentence_endings: 0
                   width: [1×1 py.int]
      replace_whitespace: 1
    <textwrap.TextWrapper object at 0x0000000007271048>

如果对象属性是数值,首先应该确认属性的数值类型:

class(tw.width)

结果:

ans =
    'py.int'

默认情况下,当将 MATLAB 数值传递给 Python 函数时,Python 会将其作为 float 类型读取。 如果函数需要一个整数,Python可能会抛出错误或产生意外结果。 此时,需要将 MATLAB 数值显式转换为整数。

tw.width = int64(3);

要调用类对象的方法:

下面语句用输入 T 创建一个封装行列表 w:

T = 'MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming.';
w = wrap(tw,T);
whos w

结果:

  Name      Size            Bytes  Class      Attributes

  w         1x2                 8  py.list   

也可以将上句改为: 

tw.wrap(T)

代码验证:

下面的代码可以将 py.list 类型的参数传递给 MATLAB 函数:

wrapped = cellfun(@char, cell(w), 'UniformOutput', false);
fprintf('%s\n', wrapped{:})

结果:

MATLAB® is a high-level language and interactive environment for
numerical computation, visualization, and programming.

参考资料:

1.MATLAB 官方文档:https://ww2.mathworks.cn/help/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值