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
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)
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)
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)
% 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
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
% 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
% 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
% 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 |
| Performance anti-patterns, vectorization, preallocation |
reference/performance-guidance.md |
| Signal processing deprecated functions |
reference/signal-processing-guidance.md |
| Audio/video I/O migration (wavread, aviread) |
reference/audio-video-guidance.md |
| Optimization toolbox (optimset, optimtool) |
reference/optimization-guidance.md |
| Control systems plot options |
reference/control-systems-guidance.md |
| Image processing ROI objects |
reference/image-processing-guidance.md |
| Statistics/ML (svmtrain, dataset, classregtree) |
reference/statistics-ml-guidance.md |
| Simulink configuration and blocks |
reference/simulink-guidance.md |
| Functions completely removed (guide, optimtool, fints, wavread) |
reference/removed-functions-guidance.md |
| Communications System objects |
reference/communications-guidance.md |
| Mapping Toolbox (webmap, wmmarker, wmline, geotiffread, mfwdtran, makerefmat) |
reference/mapping-guidance.md |
Conventions
- Always run
check_matlab_code first — let static analysis find deprecated usage
- After checkcode, scan the source for patterns checkcode misses:
subplot (not flagged), str2num (sometimes not flagged), global variables, growing arrays may only warn about size change
- Fix deprecated patterns before other code quality issues
- When writing new code, use the modern pattern from the start — don't write deprecated code and fix it later
- For functions marked "Removed" — they will cause immediate errors, not just warnings
- When migrating, test the modern replacement against the old behavior to confirm equivalence
- Consult the domain-specific reference file for detailed migration patterns with code examples
Copyright 2026 The MathWorks, Inc.
1---2name: matlab-modernize-code3description: Modernize deprecated MATLAB functions and patterns. Use when check_matlab_code or checkcode reports "not recommended" or "to be removed" warnings, when migrating legacy code, or when replacing deprecated APIs (trainNetwork, csvread, xlsread, datenum, eval, subplot, guide, optimset, wavread, svmtrain, uicontrol) with current equivalents.4license: MathWorks BSD-3-Clause5---67# Code Modernization89Replace deprecated MATLAB functions and anti-patterns with modern equivalents. This skill is the resolver — `check_matlab_code` is the detector.1011## When to Use1213- `check_matlab_code` or `checkcode` returns "not recommended" or "to be removed" warnings14- User asks to modernize, migrate, or update old MATLAB code15- Code uses functions listed in the quick reference table below16- After static analysis reveals deprecated API usage17- Writing new code in a domain that has known deprecated patterns1819## When NOT to Use2021- Reviewing code quality broadly — use `matlab-review-code` (which may then trigger this skill)22- Debugging runtime errors — use `matlab-debugging`23- Performance profiling — use performance skills (though anti-patterns below overlap)2425## Quick Reference: Top Deprecated Functions2627| Deprecated | Use instead | Since | Category |28|------------|------------|-------|----------|29| `csvread` / `dlmread` | `readmatrix` | R2019a | File I/O |30| `csvwrite` / `dlmwrite` | `writematrix` | R2019a | File I/O |31| `xlsread` | `readtable`, `readmatrix` | R2019a | File I/O |32| `xlswrite` | `writetable`, `writematrix` | R2019a | File I/O |33| `datenum` / `datestr` | `datetime` | R2014b | Date/Time |34| `subplot` | `tiledlayout` / `nexttile` | R2019b | Graphics |35| `eval` / `evalc` / `evalin` | Dynamic field names, function handles | — | Security |36| `str2num` | `str2double` | — | Security |37| `trainNetwork` | `trainnet` | R2024a | Deep Learning |38| `LayerGraph` / `SeriesNetwork` | `dlnetwork` | R2024a | Deep Learning |39| `classify` (DL) | `minibatchpredict` + `scores2label` | R2024a | Deep Learning |40| `uicontrol` | `uibutton`, `uidropdown`, etc. | R2016a | UI/App |41| `guide` | `appdesigner` | R2025a | UI (Removed) |42| `optimset` | `optimoptions` | R2013a | Optimization |43| `strmatch` | `startsWith`, `matches` | R2019b | Strings |44| `clear all` | `clearvars` | — | Performance |45| `webmap` | `geoaxes` + `geobasemap` | R2025a | Mapping |4647## Critical Anti-Patterns4849Never use these in new code:5051| Anti-pattern | Problem | Use instead |52|-------------|---------|-------------|53| `eval` / `evalc` / `evalin` | Security risk, prevents JIT optimization, difficult to debug | Dynamic field names `s.(name)`, function handles |54| `str2num` | Uses `eval` internally — code injection risk | `str2double` |55| Growing arrays in loops | O(n²) memory reallocation | Preallocate with `zeros`, `cell` |56| `global` variables | Hidden state, performance penalty | Pass as arguments or use structs |57| `clear all` | Removes functions from memory, forces recompilation | `clearvars` |58| `cd` during execution | Forces function re-resolution | `fullfile` for paths |59| `exist('var','var')` in loops | Expensive state query | Initialize variable before loop |60| Large data in code | Slow parsing, hard to maintain | Save to `.mat` or `.csv` files |6162## Modern Design Patterns6364Prefer these in all new code:6566### Table-Based Workflows67```matlab68data = readtable('sensors.csv');69data.Timestamp = datetime(data.Timestamp);70data.Status = categorical(data.Status);71recentData = data(data.Timestamp > datetime('today') - days(7), :);72summary = groupsummary(recentData, 'SensorID', 'mean', 'Value');73```7475### String Arrays (not char arrays)76```matlab77name = "John"; % not 'John'78names = ["John", "Jane", "Bob"]; % not {'John','Jane','Bob'}79fullName = firstName + " " + lastName; % not [first,' ',last]80idx = contains(names, "Jo"); % not cellfun + strfind81```8283### Arguments Block (not nargin/varargin)84```matlab85function result = processData(data, options)86 arguments87 data (:,:) double88 options.Method (1,1) string {mustBeMember(options.Method, ["fast","accurate"])} = "fast"89 options.Verbose (1,1) logical = false90 end91end92```9394### Vectorization (not loops)95```matlab96% Instead of: for i=1:n, V(i) = pi/12*(D(i)^2)*H(i); end97V = pi/12 * (D.^2) .* H;9899% Instead of: loop with if100Vgood = V(D >= 0); % logical indexing101```102103### Preallocation104```matlab105result = zeros(1, n); % numeric106C = cell(1, n); % cell array107S(n) = struct('f1', []); % struct array108```109110## Key Migrations111112### File I/O: csvread/xlsread → readmatrix/readtable113114```matlab115% Old → Modern116M = csvread('data.csv'); % M = readmatrix('data.csv');117M = dlmread('data.txt','\t'); % M = readmatrix('data.txt','Delimiter','\t');118[n,t,r] = xlsread('f.xlsx'); % T = readtable('f.xlsx');119csvwrite('out.csv', M); % writematrix(M, 'out.csv');120xlswrite('out.xlsx', data); % writetable(T, 'out.xlsx');121```122123### Deep Learning: trainNetwork → trainnet124125```matlab126% Old: classificationLayer specifies loss implicitly127net = trainNetwork(X, Y, layers, options);128129% Modern: specify loss explicitly, no classificationLayer needed130net = trainnet(X, Y, layers, "crossentropy", options);131132% Prediction133scores = minibatchpredict(net, XTest);134YPred = scores2label(scores, classNames);135```136137### eval → Dynamic Field Names / Function Handles138139```matlab140% Old: eval([varName ' = 42;']);141s.(varName) = 42;142143% Old: result = eval(['process_' method '(x)']);144handlers.fast = @processFast;145handlers.slow = @processSlow;146result = handlers.(method)(x);147```148149## References150151Load these when working in a specific domain:152153| Load when... | Reference |154|---|---|155| Deprecated core MATLAB functions (file I/O, strings, deep learning, UI) | [reference/core-functions-guidance.md](reference/core-functions-guidance.md) |156| Performance anti-patterns, vectorization, preallocation | [reference/performance-guidance.md](reference/performance-guidance.md) |157| Signal processing deprecated functions | [reference/signal-processing-guidance.md](reference/signal-processing-guidance.md) |158| Audio/video I/O migration (wavread, aviread) | [reference/audio-video-guidance.md](reference/audio-video-guidance.md) |159| Optimization toolbox (optimset, optimtool) | [reference/optimization-guidance.md](reference/optimization-guidance.md) |160| Control systems plot options | [reference/control-systems-guidance.md](reference/control-systems-guidance.md) |161| Image processing ROI objects | [reference/image-processing-guidance.md](reference/image-processing-guidance.md) |162| Statistics/ML (svmtrain, dataset, classregtree) | [reference/statistics-ml-guidance.md](reference/statistics-ml-guidance.md) |163| Simulink configuration and blocks | [reference/simulink-guidance.md](reference/simulink-guidance.md) |164| Functions completely removed (guide, optimtool, fints, wavread) | [reference/removed-functions-guidance.md](reference/removed-functions-guidance.md) |165| Communications System objects | [reference/communications-guidance.md](reference/communications-guidance.md) |166| Mapping Toolbox (webmap, wmmarker, wmline, geotiffread, mfwdtran, makerefmat) | [reference/mapping-guidance.md](reference/mapping-guidance.md) |167168## Conventions169170- Always run `check_matlab_code` first — let static analysis find deprecated usage171- **After checkcode, scan the source for patterns checkcode misses:** `subplot` (not flagged), `str2num` (sometimes not flagged), `global` variables, growing arrays may only warn about size change172- Fix deprecated patterns before other code quality issues173- When writing new code, use the modern pattern from the start — don't write deprecated code and fix it later174- For functions marked "Removed" — they will cause immediate errors, not just warnings175- When migrating, test the modern replacement against the old behavior to confirm equivalence176- Consult the domain-specific reference file for detailed migration patterns with code examples177178----179180Copyright 2026 The MathWorks, Inc.181182----183