主要内容

Call Python Function in MATLAB to Wrap Paragraph Text

This example shows how to use Python® language functions and modules within MATLAB®. The example calls a text-formatting module from the Python standard library.

MATLAB supports the reference implementation of Python, often called CPython. If you are on a Mac or Linux platform, you already have Python installed. If you are on Windows, you need to install a distribution, such as those found athttps://www.python.org/download,如果您还没有这么做的话)。更多的通知ation, seeInstall Supported Python Implementation.

Use PythontextwrapModule

MATLAB has equivalencies for much of thePython标准库,但不是一切。例如,textwrap是用于格式化带有托架返回和其他便利的文本块的模块。MATLAB还提供了textwrapfunction, but it only wraps text to fit inside a UI control.

创建一个文本段落。

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 thetextwrap.wrapfunction by typing the characterspy.in front of the function name. Do not typeimport textwrap.

包裹= py.textwrap.wrap(T); whos包裹
名称大小字节类属性包装1x3 8 py.list

包裹is a Python list, which is a list of Python strings. MATLAB shows this type aspy.list.

Convertpy.listto a cell array of Python strings.

包裹= cell(wrapped); whos包裹
Name Size Bytes Class Attributes wrapped 1x3 336 cell

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

包裹{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 thecharfunction.

包裹= 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.

以前的代码使用wrapconvenience function, but the module provides many more options using thepy.textwap.TextWrapperfunctionality. To use the options, callpy.textwap.TextWrapperwith keyword arguments described athttps://docs.python.org/2/library/textwrap.html#textwrap.textwrapper.

Create keyword arguments using the MATLABPyargsfunction with a comma-separated list of name/value pairs.width格式化文本为30个字符。这initial_indentsubsequent_indentkeywords begin each line with the comment character,%,由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.

包裹= 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从Matlab致电Python.