skills/matlab/references/programming.md
This reference targets MATLAB R2026a. Base MATLAB, separately licensed products, and GNU Octave must not be conflated.
| Artifact | Workspace | Best use | Main risk |
|---|---|---|---|
script .m | caller/base workspace | small reviewed orchestration | hidden inputs, leaked variables, path/state dependence |
function .m | local workspace | reusable computation and automation | implicit conversions or undocumented side effects |
live script .mlx | script-like interactive workspace | narrative exploration and teaching | opaque archive, embedded output, weak text review |
class .m | object state and methods | durable abstractions | constructors, listeners, serialization callbacks |
| MEX | native process code | approved performance/interface work | arbitrary native execution |
Prefer a function with explicit inputs, outputs, and an arguments block.
Keep a top-level script thin. Export reviewed live code to plain .m before
static inspection. Never open or run an untrusted project, live script, app,
class, MEX file, or package.
Scripts share the base workspace and leave variables there. Functions, including local functions, have private workspaces. Since R2024a, local functions in scripts can appear anywhere in the file except inside conditional contexts. The main function should match its filename.
function summary = summarizeSignal(signal, options)
%SUMMARIZESIGNAL Return deterministic summary statistics.
arguments
signal (:,1) double {mustBeFinite}
options.Center (1,1) logical = true
end
if options.Center
signal = signal - mean(signal);
end
summary = struct( ...
"Count", numel(signal), ...
"Mean", mean(signal), ...
"StandardDeviation", std(signal));
end
(:,1) permits a column of any height.mustBeFinite check values without changing them.arguments block alone does not
make code generation available.fullfile; never construct paths by concatenating separators.genpath because it can
expose hidden, generated, test, private, or malicious files.path, userpath, preferences, startup files, or
Java/native search paths.global, persistent cache state without invalidation, and broad
clearing. clear all can clear loaded functions and disrupt debugging.onCleanup for resources such as files and temporary state.S = load(...)) rather than injecting
names into a function or base workspace—but only after the MAT file is
trusted.MATLAB runs startup.m when it is found on the search path and finish.m on
normal exit. Projects can add paths and run startup/shutdown actions. Review all
of these before execution.
Escalate any use of:
eval, evalin, assignin, str2func, text-derived feval, dynamic
property or callback names;system, unix, dos, !);py.*, pyrun,
pyrunfile), MEX, C/C++ libraries, and generated code;loadobj, matlab.mixin.CustomElementSerialization) and
System object load hooks.Function handles are safer than text dispatch only when the handle itself comes from trusted code. Never pass untrusted text into a dispatch mechanism. Static scanning is triage, not proof of absence.
A project can track files, control the path, declare references and packages, run startup/shutdown tasks, integrate source control, and analyze dependencies. Use project APIs only after reviewing project metadata and tasks.
Recommended project record:
unknown, confirmed, or unavailable;Dependency Analyzer and matlab.codetools.requiredFilesAndProducts use static
analysis. Dynamic dispatch, overloaded methods, callbacks, generated names, and
conditional paths can cause misses or false positives. Their product lists do
not prove that a license can be checked out.
Use these only on trusted text:
codeIssues(path) returns a structured Code Analyzer result and supports
programmatic fixes for eligible issues.checkcode(path) remains useful for text-oriented or legacy automation.codeCompatibilityReport(path) finds potential issues after a release
upgrade.Do not auto-apply fixes across a scientific codebase without tests. Analyzer silence does not establish numerical correctness, security, toolbox availability, or Octave compatibility.
Migration sequence:
R2026a-specific checks include changed/removed APIs in release notes, new JSON table/timetable I/O, Python 3.13 support, string-array Python conversion, interactive HTML graphics export, and platform/compiler support. R2026a no longer ships new MATLAB releases for Intel Macs.
Base MATLAB includes script-, function-, and class-based matlab.unittest
testing. Keep tests deterministic and independent of order.
classdef TestSummarizeSignal < matlab.unittest.TestCase
methods (Test)
function centersFiniteColumn(testCase)
actual = summarizeSignal([1; 2; 3]);
testCase.verifyEqual(actual.Count, 3);
testCase.verifyEqual(actual.Mean, 0, AbsTol=1e-14);
end
function rejectsNonfiniteInput(testCase)
testCase.verifyError( ...
@() summarizeSignal([1; NaN]), ...
"MATLAB:validators:mustBeFinite");
end
end
end
Use domain-derived AbsTol and RelTol; exact checks remain appropriate for
integers, strings, dimensions, and invariants. Isolate file output in temporary
folders and refuse network, interactive dialogs, or real credentials in unit
tests.
Product boundaries:
runtests, testsuite, matlab.unittest.TestCase, and ordinary framework
plugins are base MATLAB.In R2026a, runtests automatically opens a project for tests belonging to a
project that is not already open and closes it afterward. This may execute
reviewed project startup and shutdown actions; it is not safe for untrusted
projects.