MATLAB to Python Agent with Verified Output
An LLM agent that converts MATLAB .m scripts to Python with NumPy, SciPy and Matplotlib. It checks its own work against values saved from a real MATLAB run, and retries using the exact failures when the numbers do not match.
View the code on GitHubFull results
- samples passed on the first attempt
- 4/5
- samples passed after retries
- 5/5
- MATLAB version that produced the reference outputs
- R2025b
Problem
Teams moving off MATLAB often have hundreds of analysis scripts. Translating them with an LLM fails quietly: the Python runs but the answers are slightly wrong, because of 1-based indexing, column-major reshape, or different function defaults such as filtfilt edge padding. Code that looks right is not good enough; it needs numerical proof.
Approach
- Reference outputs come from a real MATLAB run and are committed, so MATLAB is not needed afterwards.
- The agent scans the script for known built-ins and adds only the relevant rows of a 50-function mapping table to the prompt.
- A generated pytest file compares each variable with np.allclose, with per-variable tolerances.
- On failure, the next prompt carries the previous code and the exact failing checks.
Results
- Five bundled samples were checked against MATLAB R2025b at tight default tolerances, using 6 API calls in total.
- One sample failed first because the model left MATLAB indexing in the code. The agent fed the error back, and the second attempt passed every check.
- Mock mode plants realistic bugs, such as row-major reshape, so the retry path is tested without API calls.
Limitations
- Numerical checks need one MATLAB run to produce reference values.
- It handles one script at a time; multi-file projects, classes and Simulink are out of scope.
- Five samples show the loop works, not that it would reach 100% on real-world code.
- Converted scripts run locally with no sandbox, so only convert code you trust.
Built with
- Python
- NumPy
- SciPy
- Matplotlib
- pytest
- MATLAB
- Streamlit
- Gemini
- Anthropic
- OpenAI