Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/ai-and-statistics/matlab-deploy-embedded-ai

matlab-deploy-embedded-ai

Verified

Deploy AI models to embedded hardware using MathWorks tools (MATLAB, Simulink, Embedded Coder). Covers two workflow patterns: (1) MathWorks-native or imported models rebuilt as dlnetwork for lean hardware, (2) direct C/C++ code generation from PyTorch and LiteRT models. Both patterns support all targets (Cortex-M/A/R, x86, GPU). Trigger when: user wants to deploy AI to embedded targets; generate C/CUDA from neural networks; compress AI models for MCU; integrate AI in Simulink for system-level simulation; import PyTorch/ONNX/TensorFlow models for embedded deployment; optimize AI for resource-constrained hardware; or use loadPyTorchExportedProgram, loadLiteRTModel, importNetworkFromPyTorch, importNetworkFromONNX, importNetworkFromTensorFlow, importNetworkFromKeras, dlquantizer, exportNetworkToSimulink, or Embedded Coder with AI models.

references/pattern1/data-preparation.md

View on GitHub
# Data Preparation

Detailed patterns for data preparation in Embedded AI Pattern 1 (lean hardware, <500KB models).

## Data Splitting

Always set the random seed before splitting for reproducibility:

```matlab
rng("default")
```

### Recommended Split Ratios

| Dataset Size     | Problem Type        | Train / Val / Test |
|------------------|---------------------|--------------------|
| < 1,000 samples  | Tabular             | 60 / 20 / 20      |
| 1,000 - 10,000   | Tabular             | 70 / 15 / 15      |
| 10,000 - 100,000  | Tabular / Signal   | 80 / 10 / 10      |
| > 100,000         | Any                | 90 / 5 / 5        |
| < 500 images      | Image              | 60 / 20 / 20      |
| > 500 images       | Image             | 70 / 15 / 15      |

**Note:** For large datasets, validation and test sets only need to be large enough
to reliably capture the output statistics — additional data beyond that is better
used for training. The ratios above are starting points; adjust based on the
complexity of the problem and the variability in the data.

### Splitting with cvpartition (Tabular Data)

```matlab
rng("default")

% Stratified split for classification
n = height(data);
cv = cvpartition(data.Label, "HoldOut", 0.3);

trainData = data(training(cv), :);
tempData  = data(test(cv), :);

% Split remaining 30% into validation (15%) and test (15%)
cv2 = cvpartition(tempData.Label, "HoldOut", 0.5);
valData  = tempData(training(cv2), :);
testData = tempData(test(cv2), :);
```

### Splitting with splitEachLabel (Image Data)

```matlab
rng("default")

imds = imageDatastore("path/to/images", ...
    IncludeSubfolders=true, ...
    LabelSource="foldernames");

[imdsTrain, imdsVal, imdsTest] = splitEachLabel(imds, 0.7, 0.15, 0.15, "randomized");
```

### Splitting with randperm (General Purpose)

```matlab
rng("default")

n = size(X, 1);
idx = randperm(n);

nTrain = round(0.7 * n);
nVal   = round(0.15 * n);

trainIdx = idx(1:nTrain);
valIdx   = idx(nTrain+1:nTrain+nVal);
testIdx  = idx(nTrain+nVal+1:end);

XTrain = X(trainIdx, :);  YTrain = Y(trainIdx);
XVal   = X(valIdx, :);    YVal   = Y(valIdx);
XTest  = X(testIdx, :);   YTest  = Y(testIdx);
```

## Datastore Patterns (Large Data)

Use datastores when data does not fit in memory.

### tabularTextDatastore (Large CSV Files)

```matlab
tds = tabularTextDatastore("path/to/csvfiles", ...
    SelectedVariableNames=["Var1","Var2","Var3","Label"]);

tds.ReadSize = 1000;  % Rows per read
```

### imageDatastore (Large Image Datasets)

```matlab
imds = imageDatastore("path/to/images", ...
    IncludeSubfolders=true, ...
    LabelSource="foldernames");

% Apply augmentation for training
augmenter = imageDataAugmenter( ...
    RandRotation=[-20 20], ...
    RandXReflection=true);

augImds = augmentedImageDatastore([224 224 3], imds, ...
    DataAugmentation=augmenter);
```

### audioDatastore (Audio Files)

```matlab
ads = audioDatastore("path/to/audiofiles", ...
    IncludeSubfolders=true, ...
    LabelSource="foldernames");
```

### arrayDatastore (In-Memory Arrays Wrapped as Datastore)

```matlab
dsX = arrayDatastore(XTrain, IterationDimension=1);
dsY = arrayDatastore(YTrain, IterationDimension=1);
dsTrain = combine(dsX, dsY);
```

## Data Preprocessing Patterns

### Normalization

```matlab
% Z-score normalization
[XTrain, mu, sigma] = normalize(XTrain);
XVal  = normalize(XVal, "center", mu, "scale", sigma);
XTest = normalize(XTest, "center", mu, "scale", sigma);

% Save normalization parameters for deployment
save("normParams.mat", "mu", "sigma");
```

### Windowing/Segmentation for Time-Series

```matlab
% Windowing for sequence data
windowSize = 50;
stepSize   = 10;

numWindows = floor((size(data, 1) - windowSize) / stepSize) + 1;
windows = zeros(numWindows, windowSize, size(data, 2));

for i = 1:numWindows
    startIdx = (i-1) * stepSize + 1;
    windows(i, :, :) = data(startIdx:startIdx+windowSize-1, :);
end
```

### Formatting Sequence Data for trainnet

> **CRITICAL:** trainnet requires sequence data as cell arrays of **[T x C] single** matrices (time-steps x channels). The format is **[T x C], NOT [C x T]**. Getting this wrong produces an "Invalid size of channel dimension" error.

Convert loaded data to the correct format:

```matlab
% From a matrix where rows are time steps and columns are features
% Split into individual sequences (e.g., by trial or segment)
numSequences = numel(segmentIdx);
XTrain = cell(numSequences, 1);
YTrain = cell(numSequences, 1);
for i = 1:numSequences
    idx = segmentIdx{i};
    XTrain{i} = single(data(idx, featureCols));  % [T x numFeatures] single
    YTrain{i} = single(data(idx, responseCols));  % [T x numResponses] single
end

% Verify format
assert(iscolumn(XTrain), "Must be N-by-1 cell array");
assert(isa(XTrain{1}, "single"), "Each cell must be single precision");
fprintf("Sequence 1: [%d x %d] — %d time steps, %d features\n", ...
    size(XTrain{1}, 1), size(XTrain{1}, 2), size(XTrain{1}, 1), size(XTrain{1}, 2));
```

### Handling Missing Data

```matlab
% Check for missing values
missingCount = sum(ismissing(data));

% Options: remove, fill, or interpolate
data = rmmissing(data);                    % Remove rows with missing values
data = fillmissing(data, "linear");        % Linear interpolation
data = fillmissing(data, "previous");      % Forward fill
```


Copyright 2026 The MathWorks, Inc.
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 321kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 197kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall