Main Content

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 at https://www.python.org/downloads/. For more information, see Install Supported Python Implementation.

Use Python textwrap Module

MATLAB has equivalent functionality for much, but not all, of the Python standard library. 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 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.

W = py.textwrap.wrap(T);
whos W
  Name      Size            Bytes  Class      Attributes

  W         1x3                 8  py.list              

W is a Python list which MATLAB shows as type py.list. Each element is a Python string.

W{1}
ans = 
  Python str with no properties.

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

Convert py.list to a string array.

wrapped = string(W);
whos wrapped
  Name         Size            Bytes  Class     Attributes

  wrapped      1x3               514  string              
wrapped(1)
ans = 
"We at MathWorks believe in the importance of engineers and scientists."

Customize the Paragraph

Customize the output of the paragraph using keyword arguments.

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

Use the width keyword to format 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(initial_indent="% ",subsequent_indent="% ",width=int32(30));
W = wrap(tw,T);

Convert to a MATLAB argument and display the results.

message = string(W);
fprintf("%s\n", message{:})
% 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 Call Python from MATLAB.