从 MATLAB 调用 Python

MATLAB 支持 2.7、3.5、3.6 和 3.7 版本的 Python 参考实现（通常称为 CPython）。如果您在 Mac 或 Linux 平台上，会默认安装 Python。如果您在 Windows 上，则需要安装一个分发版（如果尚未安装），这种版本您可以在 https://www.python.org/download 找到。有关详细信息，请参阅安装支持的 Python 实现

调用 Python 函数使文本在段落内换行

MATLAB 具有 Python 标准库的大量等效功能，但并非全部。例如，`textwrap` 是一个模块，它可使用回车和其他便捷方式格式化文本块。MATLAB 同样提供了一个 `textwrap` 函数，但该函数只允许文本为适应 UI 控件而换行。

```T = 'MATLAB(R) is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB, you can analyze data, develop algorithms, and create models and applications. The language, tools, and built-in math functions enable you to explore multiple approaches and reach a solution faster than with spreadsheets or traditional programming languages, such as C/C++ or Java(TM).'; ```

将 Python 字符串转换为 MATLAB 字符串

```wrapped = py.textwrap.wrap(T); whos wrapped ```
``` Name Size Bytes Class Attributes wrapped 1x7 8 py.list ```

`wrapped` 是一个 Python 列表，它是一个 Python 字符串列表。MATLAB 将此类型显示为 `py.list`

`py.list` 转换为 Python 字符串的元胞数组。

```wrapped = cell(wrapped); whos wrapped ```
``` Name Size Bytes Class Attributes wrapped 1x7 840 cell ```

```wrapped{1} ```
```ans = Python str with no properties. MATLAB(R) is a high-level language and interactive environment for ```

```wrapped = cellfun(@char, wrapped, 'UniformOutput', false); wrapped{1} ```
```ans = 'MATLAB(R) is a high-level language and interactive environment for' ```

自定义段落

```tw = py.textwrap.TextWrapper(pyargs(... 'initial_indent', '% ', ... 'subsequent_indent', '% ', ... 'width', int32(30))); wrapped = wrap(tw,T); ```

```wrapped = cellfun(@char, cell(wrapped), 'UniformOutput', false); fprintf('%s\n', wrapped{:}) ```
```% MATLAB(R) is a high-level % language and interactive % environment for numerical % computation, visualization, % and programming. Using % MATLAB, you can analyze % data, develop algorithms, % and create models and % applications. The language, % tools, and built-in math % functions enable you to % explore multiple approaches % and reach a solution faster % than with spreadsheets or % traditional programming % languages, such as C/C++ or % Java(TM). ```

﻿