matlab 中textwrap,Call Python Function in MATLAB to Wrap Paragraph Text

本文介绍了如何在MATLAB中利用Python的textwrap模块来格式化文本。MATLAB虽然自带了textwrap函数,但仅限于在UI控制内进行文本包装。通过py.infront调用Python的textwrap.wrap函数,可以实现更复杂的文本格式设置,如自定义宽度、首行和后续行的缩进等。通过实例展示了将Python字符串转换为MATLAB字符串的过程,并给出了详细的操作步骤。
摘要由CSDN通过智能技术生成

Use Python textwrap Module

MATLAB has equivalencies for much of the Python standard library, but not everything. For example, textwrap is a module for formatting blocks of text with carriage returns and other conveniences. MATLAB also provides a textwrap function, but it only wraps text to fit inside a UI control.

Create a paragraph of text to play with.

T = 'We at MathWorks believe in the importance of engineers and scientists. They increase human knowledge and profoundly improve our standard of living.';

Convert Python String to MATLAB String

Call the textwrap.wrap function by typing the characters py. in front of the function name. Do not type import textwrap.

wrapped = py.textwrap.wrap(T);

whos wrapped

Name Size Bytes Class Attributes

wrapped 1x3 8 py.list

wrapped is a Python list, which is a list of Python strings. MATLAB shows this type as py.list.

Convert py.list to a cell array of Python strings.

wrapped = cell(wrapped);

whos wrapped

Name Size Bytes Class Attributes

wrapped 1x3 336 cell

Although wrapped is a MATLAB cell array, each cell element is a Python string.

wrapped{1}

ans =

Python str with no properties.

We at MathWorks believe in the importance of engineers and scientists.

Convert the Python strings to MATLAB strings using the char function.

wrapped = cellfun(@char, wrapped, 'UniformOutput', false);

wrapped{1}

ans =

'We at MathWorks believe in the importance of engineers and scientists.'

Now each cell element is a MATLAB string.

Customize the Paragraph

Customize the output of the paragraph using keyword arguments.

The previous code uses the wrap convenience function, but the module provides many more options using the py.textwap.TextWrapper functionality. To use the options, call py.textwap.TextWrapper with keyword arguments described at https://docs.python.org/2/library/textwrap.html#textwrap.TextWrapper.

Create keyword arguments using the MATLAB pyargs function with a comma-separated list of name/value pairs. width formats the text to be 30 characters wide. The initial_indent and subsequent_indent keywords begin each line with the comment character, %, used by MATLAB.

tw = py.textwrap.TextWrapper(pyargs(...

'initial_indent', '% ', ...

'subsequent_indent', '% ', ...

'width', int32(30)));

wrapped = wrap(tw,T);

Convert to a MATLAB argument and display the results.

wrapped = cellfun(@char, cell(wrapped), 'UniformOutput', false);

fprintf('%s\n', wrapped{:})

% We at MathWorks believe in

% the importance of engineers

% and scientists. They

% increase human knowledge and

% profoundly improve our

% standard of living.

Learn More

It is sufficient to remember that Python is yet another potential source of libraries for the MATLAB user. If you want to learn about moving data between MATLAB and Python, including Python data types such as tuples and dictionaries, see Python Libraries in MATLAB.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值