- Introduction
- Background
- Motivation & Objectives
- Project Architecture
- Features
- Project Structure
- Getting Started
- Usage
- How It Works
- Example Topics
- Troubleshooting & Tips
- Learning Resources
- Contributing
- License
This project demonstrates how to bridge MATLAB and Python, empowering engineers, researchers, and students to combine the strengths of both platforms. It provides practical scripts, a web-based interface, and a set of exercises to make MATLAB-Python interoperability accessible and hands-on.
MATLAB is a powerful environment for numerical computing, simulation, and visualization, widely used in engineering and science. Python has become the language of choice for data science, machine learning, and rapid prototyping due to its open ecosystem and extensive libraries. Many organizations and researchers need to use both environments together, but integration can be challenging due to differences in language, data types, and workflows.
- Motivation: MATLAB is widely used for engineering and scientific computing, while Python is popular for data science, machine learning, and rapid prototyping. Integrating them allows users to leverage the best tools from both worlds.
- Objectives:
- Enable calling MATLAB code from Python applications (not the other way around)
- Provide a web interface for remote MATLAB execution
- Offer clear, practical examples and exercises
- Make integration easy for both MATLAB and Python users
Description:
- The architecture centers on executing MATLAB code from Python, both via scripts and a web interface.
- The webapp (Flask) acts as a bridge, accepting user input and triggering MATLAB computations.
- Communication is handled through the MATLAB Engine API for Python and file-based data exchange when needed.
- Python → MATLAB integration: Run MATLAB code from Python scripts and web applications
- Web-based interface: Use a Flask app to interact with MATLAB remotely
- Data exchange: Seamlessly pass data between Python and MATLAB using supported types
- Real-world examples: Scripts for plotting, data analysis, and more
- Interactive exercises: Practice and learn integration techniques
- Presentation materials: Slides and architecture diagrams included
Examples/
matlab/
: MATLAB scripts for demonstrationpython/
: Python scripts for MATLAB integration
Exercise/
: Hands-on exerciseswebapp/
: Flask web application for remote MATLAB interactionpresentation_slides/
: Slides and diagrams
- MATLAB R2014b or later (R2019b+ recommended)
- Python 3.7+ (ensure accessible from MATLAB if needed)
- (Optional) Anaconda for environment management
- Flask and required Python packages
- Clone this repository:
git clone https://github.com/luciry/Hands-on-MATLAB-with-Python.git cd Hands-on-MATLAB-with-Python
- Install Python dependencies:
pip install -r requirements.txt
- (Optional) Set up MATLAB to use your Python:
pyenv('Version', 'C:/Path/To/python.exe')
- Go to
Examples/python/
and run the scripts. These will launch MATLAB Engine and execute MATLAB code from Python.
- Start the webapp:
python wsgi.py
- Open your browser at http://localhost:5000 and interact with MATLAB via the web interface.
- Explore the
Exercise/
folder for hands-on practice in integrating Python and MATLAB.
- MATLAB Engine API for Python: The Python scripts and webapp use the official MATLAB Engine API to launch and control MATLAB sessions from Python. This allows you to execute MATLAB functions, scripts, and exchange variables directly from Python code.
- Data Exchange: Data is passed between Python and MATLAB using native types where possible (e.g., NumPy arrays ↔ MATLAB arrays). For unsupported types, file-based exchange (e.g., .mat, .csv, .png) is used.
- Web Application: The Flask webapp provides a user-friendly interface for sending commands and data to MATLAB, and for displaying MATLAB-generated results (such as plots) in the browser.
- Calling MATLAB functions from Python
- Creating and displaying MATLAB plots via Python
- Exchanging data (arrays, scalars, images) between Python and MATLAB
- Automating MATLAB workflows from Python scripts or web requests
- Real-world use cases in engineering and data science
- Ensure your MATLAB and Python installations are compatible (see MathWorks documentation for supported versions).
- If you encounter
EngineError
or cannot start MATLAB from Python, check your environment variables and Python version. - Use
pyenv
in MATLAB to verify which Python interpreter is being used. - When exchanging complex data, prefer using
.mat
files or structured data formats. - For webapp issues, check that all dependencies are installed and that MATLAB is accessible from the server environment.
- Presentation Slides: See
presentation_slides/
for detailed explanations, diagrams, and step-by-step guides - Full Presentation: Canva Presentation
- MATLAB Engine API for Python: Official Documentation
Contributions are welcome! Please open an issue or submit a pull request with improvements, bug fixes, or new examples. For major changes, please discuss them first.
Luca Cirillo
PhD Student in Simulation and AI at the University of Genova
MATLAB Ambassador (at UniGE)
Email: [email protected]
This project is provided for educational purposes. See LICENSE
file for details (to be added).
For more details, see the example scripts in each folder, the webapp documentation, and the presentation slides.