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, versions 2.7, 3.6, and 3.7. 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/download, if you have not already done so. For more information, see Install Supported Python Implementation.
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 = '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).';
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 1x7 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 1x7 840 cell
Although wrapped
is a MATLAB cell array, each cell element is a Python string.
wrapped{1}
ans = Python str with no properties. MATLAB(R) is a high-level language and interactive environment for
Convert the Python strings to MATLAB strings using the char
function.
wrapped = cellfun(@char, wrapped, 'UniformOutput', false);
wrapped{1}
ans = 'MATLAB(R) is a high-level language and interactive environment for'
Now each cell element is a MATLAB string.
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{:})
% 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).
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.