Skill Tracer
Skills › Scientific Agent Skills › matlab › executing-scripts.md

executing-scripts.md

219 lines · skills/matlab/references/executing-scripts.md at 5f2bc03

Open on GitHub

Most marks come from matching words in the file, so treat them as pointers.

1# Command-Line Execution, Products, and Migration
2
3This reference explains reviewed execution plans. Bundled helpers never launch
4MATLAB, GNU Octave, MATLAB Engine, MEX, a compiler, or any subprocess.
5

Command-Line Execution, Products, and Migration

This reference explains reviewed execution plans. Bundled helpers never launch MATLAB, GNU Octave, MATLAB Engine, MEX, a compiler, or any subprocess.

Raw lines 1 to 5
6## Authorization gate
7
8Before execution, confirm all of the following:
9
101. every `.m` file is trusted and statically reviewed;
112. no unreviewed `.mlx`, `.fig`, `.mlapp`, MEX, MAT object, project action,
12 startup file, package, or generated artifact is reachable;
133. inputs and outputs are strict local paths with bounds and overwrite policy;
144. runtime, exact release, architecture, required products, and license are
15 confirmed;
165. shell/native/Java/.NET/Python/code-generation surfaces are approved;
176. network, credentials, displays, and external services are understood;
187. the planned argv is shown to the user and execution is explicitly approved.
20Static scan findings are not proof of safety. Never execute a file solely to
21discover what it does.

Authorization gate

Before execution, confirm all of the following:

  1. every .m file is trusted and statically reviewed;
  2. no unreviewed .mlx, .fig, .mlapp, MEX, MAT object, project action, startup file, package, or generated artifact is reachable;
  3. inputs and outputs are strict local paths with bounds and overwrite policy;
  4. runtime, exact release, architecture, required products, and license are confirmed;
  5. shell/native/Java/.NET/Python/code-generation surfaces are approved;
  6. network, credentials, displays, and external services are understood;
  7. the planned argv is shown to the user and execution is explicitly approved.

Static scan findings are not proof of safety. Never execute a file solely to discover what it does.

Raw lines 6 to 22
23## MATLAB R2026a `-batch`
25MathWorks recommends `-batch` for noninteractive command-line workflows.
26Conceptually, an approved plan looks like:
28```text
29["matlab", "-batch", "run('/reviewed/project/main.m')"]
30```
32This is argv, not an instruction to run untrusted code.
34Official R2026a behavior:
36- starts without the desktop or splash screen;
37- executes the quoted statement noninteractively;
38- logs text to standard output/error;
39- disables settings changes and toolbox caching;
40- can display figures unless paired with `-noFigureWindows` or `-nodisplay`;
41- exits automatically with code 0 on success and nonzero on failure;
42- errors if code requests interactive dialog input (except supported app-test
43 fixtures);
44- must not be combined with `-r`;
45- requires the target to be in the startup folder or on the MATLAB path.
47Use `-sd <reviewed-folder>` to set the initial folder. Do not embed untrusted
48text in a MATLAB statement. Prefer a fixed function name and JSON-validated
49scalar/list arguments converted by the planner.
51MATLAB startup still matters. On Linux, the launcher processes
52`.matlab7rc.sh`; MATLAB also runs `matlabrc.m` and the first executable
53`startup` on its path. `finish.m` can run at normal exit. A MATLAB Project can
54add paths and run startup/shutdown actions. `-sd` is not a security sandbox.
56`-r` is for interactive workflows and has not been recommended for
57noninteractive use since R2019a. Older `-r "...; exit"` patterns are easier to
58hang or mask errors.

MATLAB R2026a -batch

MathWorks recommends -batch for noninteractive command-line workflows. Conceptually, an approved plan looks like:

["matlab", "-batch", "run('/reviewed/project/main.m')"]

This is argv, not an instruction to run untrusted code.

Official R2026a behavior:

  • starts without the desktop or splash screen;
  • executes the quoted statement noninteractively;
  • logs text to standard output/error;
  • disables settings changes and toolbox caching;
  • can display figures unless paired with -noFigureWindows or -nodisplay;
  • exits automatically with code 0 on success and nonzero on failure;
  • errors if code requests interactive dialog input (except supported app-test fixtures);
  • must not be combined with -r;
  • requires the target to be in the startup folder or on the MATLAB path.

Use -sd <reviewed-folder> to set the initial folder. Do not embed untrusted text in a MATLAB statement. Prefer a fixed function name and JSON-validated scalar/list arguments converted by the planner.

MATLAB startup still matters. On Linux, the launcher processes .matlab7rc.sh; MATLAB also runs matlabrc.m and the first executable startup on its path. finish.m can run at normal exit. A MATLAB Project can add paths and run startup/shutdown actions. -sd is not a security sandbox.

-r is for interactive workflows and has not been recommended for noninteractive use since R2019a. Older -r "...; exit" patterns are easier to hang or mask errors.

Raw lines 23 to 59
60## Nonexecuting batch planner
62```bash
63python scripts/plan_batch_command.py matlab script src/main.m --root .
64python scripts/plan_batch_command.py matlab function src/analyze.m \
65 --root . --arg-json '{"value": 3}'
66python scripts/plan_batch_command.py matlab tests tests/TestAnalyze.m --root .
67```
69The planner:
71- validates a single `.m` target under `--root`;
72- rejects symlinks, URLs, traversal, `.mlx`, MEX, and oversized paths;
73- validates MATLAB identifiers and JSON values;
74- returns argv, a MATLAB statement, assumptions, and warnings;
75- marks `executes=false`;
76- never checks `PATH`, calls a runtime, reads credentials, or spawns a process.
78JSON objects become scalar structs, including fields containing empty or
79heterogeneous lists; the constructor wraps each field value in a scalar cell
80to prevent struct-array expansion. Numeric rectangular lists become numeric
81arrays. String lists become MATLAB string arrays or Octave cell arrays of
82character vectors; other lists become cells. JSON `null` becomes `[]` and an
83empty list becomes `{}`. Octave strings use single quotes with doubled internal
84apostrophes, preserving literal backslashes. Review these shapes before use.

Nonexecuting batch planner

python scripts/plan_batch_command.py matlab script src/main.m --root .
python scripts/plan_batch_command.py matlab function src/analyze.m \
  --root . --arg-json '{"value": 3}'
python scripts/plan_batch_command.py matlab tests tests/TestAnalyze.m --root .

The planner:

  • validates a single .m target under --root;
  • rejects symlinks, URLs, traversal, .mlx, MEX, and oversized paths;
  • validates MATLAB identifiers and JSON values;
  • returns argv, a MATLAB statement, assumptions, and warnings;
  • marks executes=false;
  • never checks PATH, calls a runtime, reads credentials, or spawns a process.

JSON objects become scalar structs, including fields containing empty or heterogeneous lists; the constructor wraps each field value in a scalar cell to prevent struct-array expansion. Numeric rectangular lists become numeric arrays. String lists become MATLAB string arrays or Octave cell arrays of character vectors; other lists become cells. JSON null becomes [] and an empty list becomes {}. Octave strings use single quotes with doubled internal apostrophes, preserving literal backslashes. Review these shapes before use.

Raw lines 60 to 85
Runs code python scripts/plan_batch_command.py :63
Starts an agent spawns :76
86## GNU Octave 11.3.0 plans
88The current manual documents:
90- `--eval`/`-e` to evaluate code and exit;
91- a filename argument to execute a script and exit;
92- `--no-gui`, `--quiet`, and `--no-history`;
93- `--no-init-all`/`--norc` to skip system and user initialization;
94- `--path` to add a narrow function path;
95- `--no-window-system` to disable graphics entirely.
97For deterministic reviewed plans, prefer `--no-init-all --no-history
98--quiet --no-gui`. Use `--no-window-system` only when graphics are not needed.
99Octave also has site, version, user, local `.octaverc`, and MATLAB-compatible
100`startup.m` files; skipping them changes expected user configuration and must
101be a conscious choice.
103Octave does not implement MATLAB `-batch`, Projects, or
104`matlab.unittest`. Its BIST `test` function and `%!test` blocks are different.
105Do not use an Octave result as proof that MATLAB code, graphics, toolboxes, or
106deployment will behave identically.

GNU Octave 11.3.0 plans

The current manual documents:

  • --eval/-e to evaluate code and exit;
  • a filename argument to execute a script and exit;
  • --no-gui, --quiet, and --no-history;
  • --no-init-all/--norc to skip system and user initialization;
  • --path to add a narrow function path;
  • --no-window-system to disable graphics entirely.

For deterministic reviewed plans, prefer --no-init-all --no-history --quiet --no-gui. Use --no-window-system only when graphics are not needed. Octave also has site, version, user, local .octaverc, and MATLAB-compatible startup.m files; skipping them changes expected user configuration and must be a conscious choice.

Octave does not implement MATLAB -batch, Projects, or matlab.unittest. Its BIST test function and %!test blocks are different. Do not use an Octave result as proof that MATLAB code, graphics, toolboxes, or deployment will behave identically.

Raw lines 86 to 107
108## Functions, scripts, and test entry points
110For automation:
112- prefer a main function with explicit inputs/outputs;
113- keep scripts free of base-workspace assumptions;
114- avoid current-folder dependence and broad path mutation;
115- return status through tests/errors rather than calling `exit` inside library
116 code;
117- place all output under a reviewed output root;
118- do not request interactive input.
120R2026a `runtests` automatically opens and closes a project when tests belong
121to a project not already open. Review project startup/shutdown behavior before
122using it.
124Base MATLAB has `matlab.unittest`; parallel execution requires Parallel
125Computing Toolbox. Advanced dependency selection, dashboards, generated tests,
126coverage/equivalence features can require MATLAB Test or other products.

Functions, scripts, and test entry points

For automation:

  • prefer a main function with explicit inputs/outputs;
  • keep scripts free of base-workspace assumptions;
  • avoid current-folder dependence and broad path mutation;
  • return status through tests/errors rather than calling exit inside library code;
  • place all output under a reviewed output root;
  • do not request interactive input.

R2026a runtests automatically opens and closes a project when tests belong to a project not already open. Review project startup/shutdown behavior before using it.

Base MATLAB has matlab.unittest; parallel execution requires Parallel Computing Toolbox. Advanced dependency selection, dashboards, generated tests, coverage/equivalence features can require MATLAB Test or other products.

Raw lines 108 to 127
128## Required products and license boundaries
130Separate four questions:
1321. **Static dependency:** Which products might code reference?
1332. **Installation:** Which products/add-ons are installed?
1343. **Entitlement:** Which licenses may this user/system use?
1354. **Checkout:** Which licenses are available for this run?
137`matlab.codetools.requiredFilesAndProducts` and Dependency Analyzer address
138the first question imperfectly. `license("inuse")` observes only products used
139on executed paths and itself requires launching MATLAB. None grants a license.
141Do not automatically install MATLAB or a toolbox. Downloads, installers,
142network-license configuration, and unattended automation are governed by the
143user's MathWorks account, administrator, and license terms. The R2026a Program
144Offering Guide has specific automation-server and external-application terms;
145do not paraphrase it as legal permission.

Required products and license boundaries

Separate four questions:

  1. Static dependency: Which products might code reference?
  2. Installation: Which products/add-ons are installed?
  3. Entitlement: Which licenses may this user/system use?
  4. Checkout: Which licenses are available for this run?

matlab.codetools.requiredFilesAndProducts and Dependency Analyzer address the first question imperfectly. license("inuse") observes only products used on executed paths and itself requires launching MATLAB. None grants a license.

Do not automatically install MATLAB or a toolbox. Downloads, installers, network-license configuration, and unattended automation are governed by the user's MathWorks account, administrator, and license terms. The R2026a Program Offering Guide has specific automation-server and external-application terms; do not paraphrase it as legal permission.

Raw lines 128 to 146
147## Compiler and generated-code boundaries
149- **MATLAB Compiler** creates standalone/web applications that run with a
150 release-compatible MATLAB Runtime.
151- **MATLAB Compiler SDK** creates components for external languages.
152- **MATLAB Coder** generates C/C++ source from supported MATLAB.
153- **GPU Coder, Simulink Coder, Embedded Coder**, support packages, and target
154 toolchains are separate products/capabilities.
155- A platform C/C++/Fortran compiler may also be required and must appear in
156 the current supported-compiler table.
158Building requires MATLAB plus the compiler/code-generation product and all
159products used by the source. Deployed applications can use MATLAB Runtime
160under applicable terms, but Runtime does not execute arbitrary `.m` code and
161cannot host MATLAB Engine for Python. Generated code must be verified; compiler
162success is not scientific validation.
164Never compile untrusted MATLAB, MEX, C/C++, model, or package input.

Compiler and generated-code boundaries

  • MATLAB Compiler creates standalone/web applications that run with a release-compatible MATLAB Runtime.
  • MATLAB Compiler SDK creates components for external languages.
  • MATLAB Coder generates C/C++ source from supported MATLAB.
  • GPU Coder, Simulink Coder, Embedded Coder, support packages, and target toolchains are separate products/capabilities.
  • A platform C/C++/Fortran compiler may also be required and must appear in the current supported-compiler table.

Building requires MATLAB plus the compiler/code-generation product and all products used by the source. Deployed applications can use MATLAB Runtime under applicable terms, but Runtime does not execute arbitrary .m code and cannot host MATLAB Engine for Python. Generated code must be verified; compiler success is not scientific validation.

Never compile untrusted MATLAB, MEX, C/C++, model, or package input.

Raw lines 147 to 165
166## CI design
168A safe CI design uses:
170- a pinned supported MATLAB release/update and platform;
171- an administrator-approved license configuration;
172- a reviewed project with no hidden startup action;
173- immutable source and hashed inputs;
174- a nonexecuting plan checked before the actual runner;
175- bounded time/memory/output and no interactive dialogs;
176- test results and logs that avoid environment/credential dumps;
177- product and license failures distinguished from test failures;
178- release notes and bug reports checked for the exact products.
180MathWorks provides CI integrations, but their presence does not include MATLAB
181or grant a license.

CI design

A safe CI design uses:

  • a pinned supported MATLAB release/update and platform;
  • an administrator-approved license configuration;
  • a reviewed project with no hidden startup action;
  • immutable source and hashed inputs;
  • a nonexecuting plan checked before the actual runner;
  • bounded time/memory/output and no interactive dialogs;
  • test results and logs that avoid environment/credential dumps;
  • product and license failures distinguished from test failures;
  • release notes and bug reports checked for the exact products.

MathWorks provides CI integrations, but their presence does not include MATLAB or grant a license.

Raw lines 166 to 182
183## Migration to R2026a
1851. Run `codeCompatibilityReport` and Project Upgrade on reviewed code.
1862. Run Code Analyzer and dependency analysis.
1873. Review base MATLAB and every required product's R2026a release notes,
188 compatibility considerations, supported platforms, compilers, Python, and
189 bug reports.
1904. Record reference outputs from the old release using justified tolerances.
1915. Test startup/path behavior, data import, MAT files, graphics, Python,
192 external interfaces, and deployment separately.
1936. Check R2026a platform changes such as no new Intel Mac release.
1947. Pilot before broad migration; retain rollback and provenance.
196Notable base changes relevant to this skill include Python 3.13 support and
197environment management, Python string conversion, JSON table/timetable I/O,
198interactive HTML export, faster startup and selected kernels, and project-aware
199`runtests`. Read the release notes rather than assuming this list is complete.

Migration to R2026a

  1. Run codeCompatibilityReport and Project Upgrade on reviewed code.
  2. Run Code Analyzer and dependency analysis.
  3. Review base MATLAB and every required product's R2026a release notes, compatibility considerations, supported platforms, compilers, Python, and bug reports.
  4. Record reference outputs from the old release using justified tolerances.
  5. Test startup/path behavior, data import, MAT files, graphics, Python, external interfaces, and deployment separately.
  6. Check R2026a platform changes such as no new Intel Mac release.
  7. Pilot before broad migration; retain rollback and provenance.

Notable base changes relevant to this skill include Python 3.13 support and environment management, Python string conversion, JSON table/timetable I/O, interactive HTML export, faster startup and selected kernels, and project-aware runtests. Read the release notes rather than assuming this list is complete.

Raw lines 183 to 200
201## Sources (reviewed 2026-10-01)
203- [`matlab` on Linux and `-batch`](https://www.mathworks.com/help/matlab/ref/matlablinux.html)
204- [Startup Options](https://www.mathworks.com/help/matlab/matlab_env/startup-options.html)
205- [Exit MATLAB](https://www.mathworks.com/help/matlab/matlab_env/exit-matlab.html)
206- [Run Unit Tests](https://www.mathworks.com/help/matlab/run-unit-tests.html)
207- [`runtests` R2026a behavior](https://www.mathworks.com/help/matlab/ref/runtests.html)
208- [Analyze Project Dependencies](https://www.mathworks.com/help/matlab/matlab_prog/analyze-project-dependencies.html)
209- [`requiredFilesAndProducts`](https://www.mathworks.com/help/matlab/ref/matlab.codetools.requiredfilesandproducts.html)
210- [MATLAB Compiler](https://www.mathworks.com/products/compiler.html)
211- [MATLAB Runtime](https://www.mathworks.com/products/compiler/matlab-runtime.html)
212- [Supported Compilers](https://www.mathworks.com/support/requirements/supported-compilers.html)
213- [Current system requirements](https://www.mathworks.com/support/requirements/matlab-system-requirements.html)
214- [Current Program Offering Guide](https://www.mathworks.com/help/pdf_doc/offering/offering.pdf)
215- [MATLAB release notes](https://www.mathworks.com/help/matlab/release-notes.html)
216- [Octave Command-Line Options](https://docs.octave.org/latest/Command-Line-Options.html)
217- [Scalar struct construction](https://www.mathworks.com/help/matlab/ref/struct.html)
218- [Octave string escaping](https://docs.octave.org/latest/Escape-Sequences-in-String-Constants.html)
219- [Octave Startup Files](https://docs.octave.org/latest/Startup-Files.html)

Sources (reviewed 2026-10-01)

Raw lines 201 to 219