Python Is The Ultimate MATLAB Toolbox
2022-12-17: The complete series of “Python is the Ultimate MATLAB Toolbox” articles is available as an eBook in PDF and epub formats (174 pages, Creative Commons license).
MATLAB’s ability to run Python code is fantastic—it extends MATLAB’s capabilities to encompass everything Python can do. Oddly, few MATLAB developers I know are aware of MATLAB’s binary API to Python, or of the vast possibilities Python offers MATLAB developers. I wrote Python for MATLAB Development to teach Python to MATLAB programmers and to demonstrate many of the benefits Python can bring them. My presentation at the MATLAB EXPO 2022 (slides) demonstrates the fundamentals of calling Python code from MATLAB.
Over the next few months I’ll post selections from the book that demonstrate how to call Python code from MATLAB. Prepare by setting up your MATLAB+Python environment (PfMD § 2.5). Then join me every other week on a tour of examples that show how…
Python helps MATLAB to:
- Read and write YAML, ini, TOML files
with
pyyaml
,configparser
,toml
(PfMD § 7.2-7.5). Describespy2mat.m
,mat2py.m
, (PfMD § 6.5.7),df2t.m
andt2df.m
which greatly simplify MATLAB ↔ Python data conversions. - Read and write SQLite databases in MATLAB with Python shows how to create, query, and modify SQLite databases in MATLAB using Python’s sqlite3 module. (PfMD § 7.12)
- Write richly-formatted Excel spreadsheets with openpyxl (PfMD § 7.8)
- Interact with PostgreSQL using the
psycopg2
Python module (PfMD § 7.20) - Interact with MongoDB using the
pymongo
Python module (PfMD § 7.21) - Set/get/subscribe to key-value data in Redis with
pyredis
(PfMD § 7.22) - Solve global optimization problems with
scipy.optimize.differential_evolution()
(PfMD § 11.6) - Solve simultaneous nonlinear equations with
scipy.optimize.root()
(PfMD § 11.11) - Solve integrals, derivatives, Laplace transforms, series summations symbolically with SymPy (PfMD § 11.15, 11.16)
- Accelerate MATLAB with Python and Numba (PfMD § 14.10, 14.12) ""
- Distribute MATLAB Computations To Python Running on a Cluster with Dask
dask
(PfMD § 14.14)
What about toolboxes from the MathWorks?
The MathWorks has a large collection of toolboxes that can perform most of the tasks above. Their toolboxes are the preferred solutions because they offer
- unique capability: many MATLAB toolboxes solve such complex or specialized problems that no alternatives–Python or otherwise–exist
- programming convenience: a solution coded entirely in MATLAB is easier to write, debug, and maintain
- configuration convenience: you don’t need to create a special MATLAB-friendly Python virtual environment or configure your MATLAB setup to use it
- stability: hybrid MATLAB/Python solutions are vulnerable to library compatibility problems caused by routine operating system updates. MathWorks toolboxes don’t have this problem.
Python-based solutions become attractive if you don’t have timely access to to the necessary MathWorks toolboxes. License procurement at large organizations often takes months and irritates both budget managers and the people needing the toolbox. Software purchase requests are typically challenged with questions like: Why was this need not anticipated? Which project’s funds should we cut to pay for this? Can you justify raising our overhead rates with this purchase? Is this a one-time need or will you need it again next year? If one can get organizational approval and can wait a few months, then MathWorks toolboxes are the way to go.
What about the FileExchange?
The MathWorks' code sharing site, FileExchange, hosts more than 40,000 freely available packages to supplement MATLAB, including solutions to several of the Python-powered options listed above. Why turn to Python when pure-MATLAB options exist on the FileExchange? There are several reasons:
-
Value-added Python distributions like Anaconda come bundled with a large collection of popular modules for numeric, scientific, engineering, and financial computation. If your organization supports Anaconda, chances are good that Python modules you need to augment your MATLAB work are already available.
-
Organizations that have commercial support agreements with Python distributors like Anaconda and ActiveState, or Linux providers such as Red Hat, have access to security-vetted packages from these vendors' curated repositories. Installing Python modules from these sources is a safer bet than pulling files from the FileExchange.
-
The Python community is several times larger than MATLAB’s. This means popular Python modules (including all listed above) see heavier use and so tend to be more extensively exercised and feature-rich than corresponding FileExchange options. Although project star counts are crude proxies for popularity and use, compare the number of stars in two popular MySQL client interfaces: the MATLAB MySQL Database Connector has 32 stars on the FileExchange while Python’s PyMySQL module (which itself is less popular than the official MySQL connector released by the MySQL development team) has 6,800 stars on Github.
-
Open source Python modules tend to be developed, discussed, tracked, and released on code collaboration sites such as Github, Gitlab, or SourceForge. These allow one to report and track bugs, track forks, follow code commits, and view code in the browser. In contrast, the FileExchange is essentially a storage location with options for comments. Compared to the collaboration sites, the FileExchange offers less insight on a project’s vitality, code content, or number of contributors.