|
| 1 | +"""Simple test script for MATLAB engine functionality.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from pathlib import Path |
| 5 | +import sys |
| 6 | + |
| 7 | +# Add src directory to Python path |
| 8 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 9 | + |
| 10 | +from matlab_mcp.engine import MatlabEngine |
| 11 | + |
| 12 | + |
| 13 | +async def test_engine(): |
| 14 | + """Test basic MATLAB engine functionality.""" |
| 15 | + engine = MatlabEngine() |
| 16 | + |
| 17 | + try: |
| 18 | + # Initialize engine |
| 19 | + await engine.initialize() |
| 20 | + |
| 21 | + # Run a simple MATLAB command |
| 22 | + result = await engine.execute('2 + 2') |
| 23 | + print(f"2 + 2 = {result.output}") |
| 24 | + |
| 25 | + # Try plotting |
| 26 | + script_path = Path(__file__).parent / "matlab_scripts" / "test_plot.m" |
| 27 | + if script_path.exists(): |
| 28 | + print(f"\nExecuting plot script: {script_path}") |
| 29 | + result = await engine.execute( |
| 30 | + str(script_path), |
| 31 | + is_file=True, |
| 32 | + capture_plots=True |
| 33 | + ) |
| 34 | + print(f"Generated {len(result.figures)} figures") |
| 35 | + |
| 36 | + # Save figures |
| 37 | + output_dir = Path("test_output") |
| 38 | + output_dir.mkdir(exist_ok=True) |
| 39 | + |
| 40 | + for i, fig in enumerate(result.figures): |
| 41 | + ext = ".png" if fig.format.value == "png" else ".svg" |
| 42 | + output_file = output_dir / f"figure_{i}{ext}" |
| 43 | + output_file.write_bytes(fig.data) |
| 44 | + print(f"Saved {output_file}") |
| 45 | + |
| 46 | + except Exception as e: |
| 47 | + print(f"Error: {str(e)}") |
| 48 | + finally: |
| 49 | + engine.close() |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + asyncio.run(test_engine()) |
0 commit comments