matlab-modernize-code
This skill modernizes MATLAB code by replacing deprecated functions and anti-patterns with current equivalents. Use it when static analysis tools like `checkcode` flag "not recommended" or "to be removed" warnings, or when migrating legacy code that uses outdated APIs such as `csvread`, `subplot`, `eval`, or `datenum`. It serves as the automated resolver paired with the `check_matlab_code` detector skill.
git clone --depth 1 https://github.com/matlab/matlab-agentic-toolkit /tmp/matlab-modernize-code && cp -r /tmp/matlab-modernize-code/skills-catalog/matlab-software-development/matlab-modernize-code ~/.claude/skills/matlab-modernize-codeSKILL.md
# Code Modernization
Replace deprecated MATLAB functions and anti-patterns with modern equivalents. This skill is the resolver — `check_matlab_code` is the detector.
## When to Use
- `check_matlab_code` or `checkcode` returns "not recommended" or "to be removed" warnings
- User asks to modernize, migrate, or update old MATLAB code
- Code uses functions listed in the quick reference table below
- After static analysis reveals deprecated API usage
- Writing new code in a domain that has known deprecated patterns
## When NOT to Use
- Reviewing code quality broadly — use `matlab-review-code` (which may then trigger this skill)
- Debugging runtime errors — use `matlab-debugging`
- Performance profiling — use performance skills (though anti-patterns below overlap)
## Quick Reference: Top Deprecated Functions
| Deprecated | Use instead | Since | Category |
|------------|------------|-------|----------|
| `csvread` / `dlmread` | `readmatrix` | R2019a | File I/O |
| `csvwrite` / `dlmwrite` | `writematrix` | R2019a | File I/O |
| `xlsread` | `readtable`, `readmatrix` | R2019a | File I/O |
| `xlswrite` | `writetable`, `writematrix` | R2019a | File I/O |
| `datenum` / `datestr` | `datetime` | R2014b | Date/Time |
| `subplot` | `tiledlayout` / `nexttile` | R2019b | Graphics |
| `eval` / `evalc` / `evalin` | Dynamic field names, function handles | — | Security |
| `str2num` | `str2double` | — | Security |
| `trainNetwork` | `trainnet` | R2024a | Deep Learning |
| `LayerGraph` / `SeriesNetwork` | `dlnetwork` | R2024a | Deep Learning |
| `classify` (DL) | `minibatchpredict` + `scores2label` | R2024a | Deep Learning |
| `uicontrol` | `uibutton`, `uidropdown`, etc. | R2016a | UI/App |
| `guide` | `appdesigner` | R2025a | UI (Removed) |
| `optimset` | `optimoptions` | R2013a | Optimization |
| `strmatch` | `startsWith`, `matches` | R2019b | Strings |
| `clear all` | `clearvars` | — | Performance |
| `webmap` | `geoaxes` + `geobasemap` | R2025a | Mapping |
## Critical Anti-Patterns
Never use these in new code:
| Anti-pattern | Problem | Use instead |
|-------------|---------|-------------|
| `eval` / `evalc` / `evalin` | Security risk, prevents JIT optimization, difficult to debug | Dynamic field names `s.(name)`, function handles |
| `str2num` | Uses `eval` internally — code injection risk | `str2double` |
| Growing arrays in loops | O(n²) memory reallocation | Preallocate with `zeros`, `cell` |
| `global` variables | Hidden state, performance penalty | Pass as arguments or use structs |
| `clear all` | Removes functions from memory, forces recompilation | `clearvars` |
| `cd` during execution | Forces function re-resolution | `fullfile` for paths |
| `exist('var','var')` in loops | Expensive state query | Initialize variable before loop |
| Large data in code | Slow parsing, hard to maintain | Save to `.mat` or `.csv` files |
## Modern Design Patterns
Prefer these in all new code:
### Table-Based Workflows
```matlab
data = readtable('sensors.csv');
data.Timestamp = datetime(data.Timestamp);
data.Status = categorical(data.Status);
recentData = data(data.Timestamp > datetime('today') - days(7), :);
summary = groupsummary(recentData, 'SensorID', 'mean', 'Value');
```
### String Arrays (not char arrays)
```matlab
name = "John"; % not 'John'
names = ["John", "Jane", "Bob"]; % not {'John','Jane','Bob'}
fullName = firstName + " " + lastName; % not [first,' ',last]
idx = contains(names, "Jo"); % not cellfun + strfind
```
### Arguments Block (not nargin/varargin)
```matlab
function result = processData(data, options)
arguments
data (:,:) double
options.Method (1,1) string {mustBeMember(options.Method, ["fast","accurate"])} = "fast"
options.Verbose (1,1) logical = false
end
end
```
### Vectorization (not loops)
```matlab
% Instead of: for i=1:n, V(i) = pi/12*(D(i)^2)*H(i); end
V = pi/12 * (D.^2) .* H;
% Instead of: loop with if
Vgood = V(D >= 0); % logical indexing
```
### Preallocation
```matlab
result = zeros(1, n); % numeric
C = cell(1, n); % cell array
S(n) = struct('f1', []); % struct array
```
## Key Migrations
### File I/O: csvread/xlsread → readmatrix/readtable
```matlab
% Old → Modern
M = csvread('data.csv'); % M = readmatrix('data.csv');
M = dlmread('data.txt','\t'); % M = readmatrix('data.txt','Delimiter','\t');
[n,t,r] = xlsread('f.xlsx'); % T = readtable('f.xlsx');
csvwrite('out.csv', M); % writematrix(M, 'out.csv');
xlswrite('out.xlsx', data); % writetable(T, 'out.xlsx');
```
### Deep Learning: trainNetwork → trainnet
```matlab
% Old: classificationLayer specifies loss implicitly
net = trainNetwork(X, Y, layers, options);
% Modern: specify loss explicitly, no classificationLayer needed
net = trainnet(X, Y, layers, "crossentropy", options);
% Prediction
scores = minibatchpredict(net, XTest);
YPred = scores2label(scores, classNames);
```
### eval → Dynamic Field Names / Function Handles
```matlab
% Old: eval([varName ' = 42;']);
s.(varName) = 42;
% Old: result = eval(['process_' method '(x)']);
handlers.fast = @processFast;
handlers.slow = @processSlow;
result = handlers.(method)(x);
```
## References
Load these when working in a specific domain:
| Load when... | Reference |
|---|---|
| Deprecated core MATLAB functions (file I/O, strings, deep learning, UI) | [reference/core-functions-guidance.md](reference/core-functions-guidance.md) |
| Performance anti-patterns, vectorization, preallocation | [reference/performance-guidance.md](reference/performance-guidance.md) |
| Signal processing deprecated functions | [reference/signal-processing-guidance.md](reference/signal-processing-guidance.md) |
| Audio/video I/O migration (wavread, aviread) | [reference/audio-video-guidance.md](reference/audio-video-guidance.md) |
| Optimization toolbox (optimset, optimtool) | [reference/optimization-guidance.md](reference/optimization-guidance.md) |
| Control system>
Import recorded driving sensor data (GPS, camera, lidar, actor tracks, lanes) into scenariobuilder.* objects (GPSData, CameraData, LidarData, ActorTrackData, Trajectory, laneData) and run preprocessing — synchronize, offset correction, crop, normalizeTimestamps, convertTimestamps. Also: compute actor tracks from lidar when no annotations exist, attach camera/lidar mounting + intrinsics, export to MAT/workspace/timetable/script. Use for raw driving dataset files (KITTI, nuScenes, Waymo, Pandaset, ROS/ROS2 bags, .mat, .csv, .mp4) or driving/vehicle/sensor logs that need wrapping. drivingLogAnalyzer (DLA) is OPT-IN ONLY — invoke only on explicit user request ('DLA', 'open in DLA', 'inspect/explore/analyze the recording') or reported sensor problem (sync drift, timestamp mismatch, overlay misalignment). NEVER auto-launch DLA after wrapping (Rule 0). For 'build scenario / export to RoadRunner / drivingScenario / OpenSCENARIO / Unreal / simulate', hand off to matlab-scenario-builder.
Generate driving scenes, scenarios, road surfaces, and 3D content from already-wrapped scenariobuilder.* sensor data (GPS, camera, lidar, actor tracks) using Scenario Builder for Automated Driving Toolbox. Use to BUILD, EXPORT, or AUGMENT a virtual scenario/scene/map: ego or actor trajectories, trajectory smoothing, OpenCRG road-surface extraction, 3D asset generation, static-object placement, point-cloud georeferencing + elevation, lane-based ego localization, sensor-fusion tracking, scenario-event extraction (cut-ins, hard brakes, near-misses, ADAS disengagements), or export to RoadRunner, drivingScenario, OpenDRIVE, OpenCRG, OpenSCENARIO, or Unreal Engine. Also: log-to-scenario, scenario harvesting, accident/near-miss reconstruction, SOTIF (ISO 21448) and ISO 26262 scenario coverage, USGS-aerial-lidar scene augmentation, traffic-sign placement from camera+lidar logs. NOT for raw-data import or multi-sensor sync/crop/offset/timestamp normalization — route those to matlab-driving-data-importer.
>
>
>
>
Build, modify, and diagram SimBiology models — API reference, helper functions, and layout patterns. Use when constructing or editing models programmatically or visually.