Calls Python libraries from MATLAB using py.*, pyrun, pyrunfile, or pyenv. Handles Python errors in MATLAB and sets up Python environments for MATLAB.
Ships in matlab-external-language-interfaces. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Call Python libraries from MATLAB using modern APIs, correct environment setup, and structured error recovery.
py.*, pyrun, or pyrunfileModuleNotFoundError, Python is not configured, or
other Python-related errorspyargsUse MATLAB name=value syntax for Python keyword arguments. pyargs is outdated
(name=value available since R2021a).
% TEMPLATE — not executable (requires scikit-learn)
rfc = py.sklearn.ensemble.RandomForestClassifier(n_estimators=int32(100), random_state=int32(42));
pyversionpyversion is not recommended. Use pyenv, which supports terminate, ExecutionMode,
and environment switching.
Do not use MATLAB system(). The Bash tool provides streaming output and avoids
complex string escaping.
BEFORE running pip install or creating virtual environments, follow
references/environment-setup.md. Do NOT infer Python paths from error
tracebacks — those paths show importlib internals, not the correct install
target.
terminate(pyenv) restarts the Python process and clears all Python state
(imported modules, variables, open connections). Always inform the user what
will be lost and get explicit consent before calling terminate.
MATLAB passes double by default. Python functions expecting int receive float,
causing TypeError. Use int32() for counts, indices, and size arguments. If the
value won't fit in 32 bits, use int64() or uint64() to prevent overflow.
pystringarray for string arrays (R2026a or later)When passing MATLAB string arrays to Python in R2026a or later, use
pystringarray. Requires NumPy 2.0 or later. In R2025b and earlier, or when
NumPy < 2.0, use py.list(cellstr(...)).
% R2026a and later:
names = ["Alice", "Bob", "Charlie"];
pyNames = pystringarray(names);
% R2025b and earlier:
names = ["Alice", "Bob", "Charlie"];
pyNames = py.list(cellstr(names));
When calling Python from MATLAB, follow this workflow.
result = py.module.function(arg1, arg2, kwarg1="value", kwarg2=42);
Prefer py.module.function(...) for direct function calls — it's readable,
integrates with the MATLAB workspace, and supports tab completion. Reserve
pyrun for multi-statement scripts where variables and imports persist in
Python memory across calls (REPL-like execution). Reserve pyrunfile for
running a standalone .py file (equivalent to running from the command line;
no state carries over).
Execute via mcp__matlab__evaluate_matlab_code (for inline code) or
mcp__matlab__run_matlab_file (for script files).
Follow references/environment-setup.md ONLY for environment-related errors:
ModuleNotFoundErrorpy.* name (e.g., "Unable to resolve the name
'py.numpy'")pyenv().Executable is emptypy or pyenv calls error stating environment is not supportedFor all other Python errors (ValueError, TypeError, logic errors in the Python code itself) — debug the code directly. These are code bugs, not environment issues.
| Function | Purpose | Available From |
|---|---|---|
pyenv | Query/configure Python environment | R2019b |
pyrun | Execute Python statements; variables persist across calls | R2021b |
pyrunfile | Execute a standalone .py file | R2021b |
pystringarray | Pass MATLAB string array to Python | R2026a |
terminate(pyenv) restarts Python without restarting
MATLAB. Can switch environments freely.In ExecutionMode="OutOfProcess", when working on a custom Python module and you
need to make changes after loading the module, or when you install packages
mid-session, ALWAYS call terminate(pyenv) before calling the module again.
In ExecutionMode="InProcess", when working on a custom Python module and you
need to make changes after loading the module, ALWAYS reload the module by running:
clear all;
clear classes;
mod = py.importlib.import_module("<module_name>");
py.importlib.reload(mod);
rfc = py.sklearn.ensemble.RandomForestClassifier(n_estimators=int32(100), random_state=int32(42));
rfc.fit(XTrain, yTrain);
predictions = double(rfc.predict(XTest));
After creating a virtual environment (venv) using the Bash tool, point MATLAB at it:
% Terminate current Python if loaded
terminate(pyenv);
% Point at venv executable
pyenv(Version="/path/to/.venv/Scripts/python.exe");
% Verify
disp(pyenv().Executable);
py.numpy.array([1, 2, 3]);
Val
(e.g., py.complex(imag=imagVal) not py.complex(imag=imag))pyenv(Version=...) at the venv executable directly.Copyright 2026 The MathWorks, Inc.
What SKILL.md sets; muted values are Claude Code's defaults
/matlab-external-language-interfaces:matlab-call-pythonfrom matlab-external-language-interfaces
npx claudepluginhub matlab/matlab-agentic-toolkit --plugin matlab-external-language-interfaces3 skills in this plugin
/matlab-configure-javaCheck which Java version MATLAB is using, switch it, or reset it without accidentally starting Java. Use this skill when a user wants to determine which Java/JDK/JRE version MATLAB is using, configure MATLAB to use a different JDK, or revert to the MATLAB default Java, or when a MATLAB script errors on a java.* API call ("no runtime environment for Java applications has been found", "Unable to find Java library"). Covers jenv and matlab_jenv, R2025a+ dynamic loading, restart requirements based on Java load status, and release- and platform-specific reset behavior. Also handles installing OpenJDK (the MATLAB Add-Ons Explorer, Adoptium, or a command-line mpm install through matlab-core:matlab-install-products).
/matlab-upgrade-mex-icUpgrade C, C++, and Fortran MEX source files from the Separate Complex (SC) API to the Interleaved Complex (IC) API. Use when converting mxGetPr/mxGetPi to mxGetComplexDoubles, migrating MEX files to -R2018a, adding MX_HAS_INTERLEAVED_COMPLEX guards for SC/IC guarded builds, or modernizing legacy MEX code that uses mxGetData/mxSetData/mxGetImagData/mxSetImagData. Covers C (.c), C++ (.cpp, .cxx), and Fortran (.F, .f90) MEX functions. Triggers on: interleaved complex, IC MEX, mex upgrade, mex migration, separate complex, mxGetPi, mxGetPr replacement, -R2018a, complex MEX, Fortran MEX, .F MEX file, C++ MEX, .cpp MEX file, MEX performance, MEX slow complex, MEX call overhead, improve MEX performance complex.
Other plugins' skills for the same job