Transform your MATLAB workflow with AI-powered automation
A Python-based toolkit that leverages AI to generate, validate, and execute MATLAB simulations. Seamlessly bridge modern LLM-driven workflows (GPT-4) with traditional MATLAB scientific computing.
- 🧠 AI-Powered Code Generation
Generate clean, production-ready MATLAB scripts with GPT-4 via natural language prompts. - ✅ Automatic Code Validation
Catch warnings & errors with MATLAB'smlint(checkcode) and display line-by-line feedback. - 🚀 Real-Time Simulation
Stream and capture simulation outputs, plots, and figures via MATLAB Engine & TCP/IP. - 🔄 Error Correction Loop
Auto-fix common syntax & runtime errors by feeding MATLAB error messages back to the LLM. - 🖥️ Interactive CLI
Engage in an AI-guided REPL to iteratively refine, validate, and run your MATLAB code.
# 1. Clone and navigate
git clone https://github.com/marcomelloni/matlab-ai-agent
cd matlab-ai
# 2. Install dependencies with Poetry
poetry install
# 3. Set your OpenAI API key
echo "OPENAI_API_KEY=your_api_key_here" > .env
# 4. Launch and start creating!
poetry run matlab-ai-agentThat's it! Start transforming your ideas into working MATLAB code instantly.
- Python 3.8+
- MATLAB R2024a or later (with MATLAB Engine for Python installed)
- OpenAI API Key (for GPT-4 integration)
- Poetry (≥1.5.0) for environment & dependency management
git clone https://github.com/marcomelloni/matlab-ai-agent
cd matlab-aiEnsure Poetry is installed on your system. If not already installed, execute the following commands:
python3 -m pip install --user pipx
python3 -m pipx ensurepath
pipx install poetryVerify the installation:
poetry --versionActivate the virtual environment:
poetry env activateImportant:
The commandpoetry env activateprovides the activation command which must be executed separately.
Copy and run the displayed command, for example:
source /path/to/virtualenv/bin/activateVerify environment activation:
which pythonInstall all dependencies defined in pyproject.toml:
poetry installTo integrate MATLAB with Python, install the MATLAB Engine API within your Poetry environment using one of these methods:
Using the package included with your MATLAB installation (example for MATLAB R2024b on MacOS):
cd /Applications/MATLAB_R2024b.app/extern/engines/python
poetry run python -m pip install .Note: Adjust the path according to your MATLAB version and installation location.
Alternatively, use the version-specific matlabengine pip package:
pip install matlabengine==24.2.2Select the version compatible with your MATLAB release from the package history.
Confirm successful installation with:
poetry run python -c "import matlab.engine; print('MATLAB Engine is installed successfully')"Expected output:
MATLAB Engine is installed successfully!
Create a .env file in the project root with your OpenAI API key:
OPENAI_API_KEY=your_api_key_here
To enable advanced features of the MATLAB AI Agent, run:
poetry run matlab-ai-agent --generate-promptThis command creates a .matlab_ai_prompt file inside the root directory.
In this file, you can define custom rules and constraints that the agent will follow during code validation and execution.
- Required parameters for simulations
- MATLAB functions to avoid (e.g., for performance or security)
- Numeric or logical constraints
- Optimization goals
- Required outputs (e.g., plots, logs, result files)
- Code style or formatting rules
- Any other execution guideline
- Maximum simulation time: 60 seconds
- Avoid using "eval" and "assignin"
- Save key results to "result.mat"
- Save plots as "output.png" if generated
- Prefer vectorized operations
ℹ️ The
.matlab_ai_promptfile is loaded automatically at runtime. To update rules, simply edit the file—no need to rerun the command.
Launch an AI-guided interactive session for generating, validating, and executing MATLAB code:
poetry run matlab-ai-agentOptions:
--matlab / --no-matlab: Enable/disable MATLAB engine for validation and execution (enabled by default)
Interactive capabilities:
- Generate MATLAB code from natural language descriptions
- Validate code with mlint and automatically correct errors
- Execute simulations and diagnose issues
- Refine code through additional prompts
- Save generated code to .m files
- Exit with option to close the MATLAB engine
Execute existing MATLAB files:
poetry run matlab-ai-agent execute path/to/file.mParameters:
<filepath>: Path to the .m file to execute
The file is executed via the MATLAB engine, with output and simulation results displayed upon successful execution.
The developer-specific commands are:
# Run test suite
poetry run pytest
# Run linting checks
poetry run pylint src
# Format code automatically
poetry run autopep8 --in-place --aggressive --recursive .This project is licensed under a modified MIT License.
Commercial use is not permitted without explicit permission.
See the LICENSE file for details.
