matlab-prepare-signal-data

$npx mdskill add matlab/matlab-agentic-toolkit/matlab-prepare-signal-data

Builds signalDatastore pipelines for ML training from signal files.

  • Derives labels from filenames, folders, or in-file columns.
  • Depends on Signal Processing Toolbox and MATLAB parallel computing.
  • Triggers on function names like signalDatastore or workflow phrases.
  • Outputs a datastore ready for trainnet with optional framing and splitting.

SKILL.md

.github/skills/matlab-prepare-signal-dataView on GitHub ↗
---
name: matlab-prepare-signal-data
description: |
  Use this skill when building a `signalDatastore` pipeline for ML training:
  loading signals from .mat / .csv / .dat folders (and `.wav` when Audio
  Toolbox is unavailable), deriving labels (filename, folder, in-file
  column, ROI), splitting into train/val/test, framing long signals for
  per-frame supervision, parallel processing across a parpool, or shaping
  a datastore output for `trainnet`. Triggers include the function names
  `signalDatastore`, `filenames2labels`, `folders2labels`, `splitlabels`,
  `countlabels`, `framesig`, `framelbl`, `signalMask`, `catmask`, and
  workflow phrases like "labels from filenames", "stratified split",
  "ReadFcn for signalDatastore", "load mat/csv/wav for training".
license: MathWorks BSD-3-Clause
metadata:
  author: MathWorks
  version: "1.0"
---

# Prepare Signal Data for ML Training

> **Look in Signal Processing Toolbox first.** The labeling, splitting,
> framing, and partitioning helpers for `signalDatastore` live in Signal
> Processing Toolbox — not in Stats & ML Toolbox or generic-MATLAB string
> utilities.

## When to Use

Loading or preparing signal / time-series data for ML training in MATLAB.
Reach for this skill especially when you want:

- labels derived from filenames or folder names
- stratified train/val/test splits over a datastore
- per-frame labels from ROI tables on long signals
- parallel processing of signals across a parpool
- a datastore shaped to feed `trainnet`

## When NOT to Use

- **Raw `.wav` audio classification with Audio Toolbox available.**
  `audioDatastore` is the canonical path. If Audio Toolbox is not
  available, this skill's custom-`ReadFcn` workflow handles `.wav`
  via base-MATLAB `audioread` — see references/wf-custom-readfcn.md.

## Best practices

- **Deliverable is a runnable `.m` script the user can save and re-run** —
  not workspace state. The output of this skill is a reusable data-prep
  pipeline the user can version and hand off.

## § 0 Common reflexes

If your first instinct is one of these, the canonical replacement is one
row away.

| Reflex | Canonical | Detail |
|---|---|---|
| Custom `ReadFcn` for a `.csv` | `signalDatastore` default reader + `SignalVariableNames` | references/fn-signaldatastore.md |
| `cvpartition` for a datastore split | `splitlabels` + `subset(ds, idx{k})` (cell-array indexing) | references/fn-splitlabels.md |
| `regexp` / `extractBefore` / `fileparts` to derive labels from filenames | `filenames2labels(sds, Extract=...)` | references/fn-filenames2labels.md |
| `regexp` / hand-rolled `fileparts(fileparts(...))` for labels from subfolders | `folders2labels(sds.Files)` — pass the datastore's file list | references/fn-folders2labels.md |
| `parfor i = 1:numel(ds.Files)` constructing a fresh datastore per file | `partition(ds, N, k)` per worker | references/fn-partition.md, references/wf-parallel-process.md |
| Manual framing loop with `(i-1)*hopSize+1` | `framesig(x, fl, OverlapLength=...)` | references/fn-framesig.md, references/wf-frame-and-label.md |
| Manual ROI-to-frame label vote with `containers.Map` | `framelbl(rois, ConsolidationMethod=..., PriorityList=...)` | references/fn-framelbl.md, references/wf-frame-and-label.md |
| `for` loop calling `load(file)` to extract labels from in-file variables | `signalDatastore(folder, SignalVariableNames=["x","label"])` — **the loop goes away**; both variables come back per `read(sds)` as a cell row | references/fn-signaldatastore.md |

## § 1 Workflows

Each workflow file is the **entry point**; it links the function-detail
files you'll need at each step.

| Workflow | Use when | Reference (entry → chain) |
|---|---|---|
| **Load + label + split** | Building a datastore from a folder of files for training. | wf-load-and-split.md → fn-signaldatastore, fn-filenames2labels / fn-folders2labels, fn-countlabels, fn-splitlabels, fn-subset |
| **Frame long signals + per-frame labels** | Signals are long; supervision is per-window. | wf-frame-and-label.md → fn-framesig, fn-framelbl, fn-signalmask-getmask |
| **Parallel processing across a parpool** | Computing per-signal results across workers. | wf-parallel-process.md → fn-partition |
| **Custom ReadFcn (only when needed)** | File format isn't `.mat` / `.csv`, or has a metadata prelude. | wf-custom-readfcn.md → fn-signaldatastore |
| **Hand-off to `trainnet`** | Datastore is ready; next step is shaping for `trainnet` / `combine` / `arrayDatastore` (routes to `minibatchqueue` / `dlarray` for custom batching or GPU prefetching). | wf-handoff-to-dl.md |

## § 2 Functions

| Function | Used for | Reference |
|---|---|---|
| `signalDatastore` | Datastore constructor (.mat / .csv / custom). | references/fn-signaldatastore.md |
| `filenames2labels` | Categorical labels from filename pattern. | references/fn-filenames2labels.md |
| `folders2labels` | Categorical labels from containing-folder name. | references/fn-folders2labels.md |
| `splitlabels` | Stratified train/val/test index sets. | references/fn-splitlabels.md |
| `countlabels` | Per-class file count for balance checks. | references/fn-countlabels.md |
| `subset` | Slice a datastore by index (single-process). | references/fn-subset.md |
| `partition` | Slice a datastore across parpool workers. | references/fn-partition.md |
| `framesig` | Frame a signal into windows with overlap. | references/fn-framesig.md |
| `framelbl` | Collapse ROI rows into per-frame labels. | references/fn-framelbl.md |
| `signalMask` / `catmask` / `binmask` | Per-sample masks from ROI tables. | references/fn-signalmask-getmask.md |

## § 3 Highest-frequency canonical patterns (inline)

### 3.1 CSV is first-class — no custom ReadFcn for tabular CSV

```matlab
sds = signalDatastore(folder, ...
    FileExtensions=".csv", ...
    SignalVariableNames=["ch1","ch2"]);
```

**Don't** wrap `readtable(..., 'SelectedVariableNames', ...)` in a custom
`ReadFcn`. The default reader does this directly.
Full table: references/fn-signaldatastore.md.

### 3.2 Filename labels — position-independent extraction

```matlab
labels = filenames2labels(sds, Extract = "G" + digitsPattern);
```

**Don't** reach for `extractBefore("_")` / `regexp` — silent wrong labels
when filename format varies.
More patterns: references/fn-filenames2labels.md.

### 3.3 Stratified split — splitlabels + subset

```matlab
splitIndices = splitlabels(labels, [0.7 0.15 0.15]);
sdsTrain = subset(sds, splitIndices{1});
sdsVal   = subset(sds, splitIndices{2});
sdsTest  = subset(sds, splitIndices{3});
% splitIndices{4} exists but is empty here (ratios sum to 1).
```

`splitlabels` returns an `(N+1)`-element **cell array** of index vectors. When `sum(ratios) == 1` (as above) the last cell is empty; when `sum(ratios) < 1` the last cell holds the leftover indices.

**Don't** use `cvpartition` for datastore-backed splits — requires materializing
labels first and doesn't compose with `subset`.
Full contract: references/fn-splitlabels.md.


----

Copyright 2026 The MathWorks, Inc.

----

More from matlab/matlab-agentic-toolkit

SkillDescription
matlab-access-datafeed>
matlab-add-awgnRead BEFORE writing any code that adds Additive White Gaussian Noise (AWGN) to signals and converts between SNR, Eb/No, Es/No, and per-subcarrier SNR for communications simulations, using awgn(), convertSNR(), berawgn(). The default MATLAB patterns for AWGN (e.g., 'measured' option, manual SNR formulas) produce subtly incorrect results. This skill specifies the correct calling conventions, required function usage, and critical anti-patterns that must be avoided.
matlab-analyze-ams-waveformAnalyze AMS waveform data using Mixed-Signal Blockset utilities: phase noise measurement, clock jitter, anti-aliased resampling, timing measurements, lock time, INL/DNL, ADC/DAC calibration, HSpice import. Use when analyzing time-domain voltage from PLL/VCO/clock simulations, measuring phase noise from variable-step solver output, computing jitter, or resampling non-uniform data.
matlab-analyze-dataAnalyze data using MATLAB. Use when the task involves tables, timetables, time-series data, numeric arrays, sensor matrices, or gridded data — including but not limited to exploring, filtering, sorting, cleaning, transforming, aggregating, smoothing, padding, trimming, and answering questions about data. MATLAB provides extensive, easy-to-use built-in functions for these workflows with no additional products required.
matlab-analyze-dependenciesAnalyze the effective toolbox file set to produce a Dependency Manifest — classify all transitive dependencies as included, product, add-on, or external-unresolved, then present resolution options with tradeoffs. Use after matlab-define-toolbox-api when the spec is approved.
matlab-analyze-emS-parameters, insertion loss, fields, currents, mesh control, and solver selection for RF PCB performance validation. TRIGGER: user asks to compute S-parameters, analyze insertion/return loss, extract fields or currents, compare MoM vs FEM, or control mesh for any RF PCB component. Invoke BEFORE writing sparameters() or solver code — API is non-obvious. SKIP: designing or creating components (use the specific matlab-design-pcb-* skill), material/stackup setup only (use matlab-manage-pcb-material), optimization sweeps (use matlab-optimize-pcb-design), PDN/IR-drop analysis (use matlab-analyze-pcb-pdn).
matlab-analyze-installed-antennaAnalyze antennas installed on electrically large conducting platforms using MATLAB Antenna Toolbox. Loads platform geometry from STL/STEP/IGES, installs antenna elements, selects electromagnetic solvers (MoM-PO, FMM, MoM), and computes patterns, impedance, coupling, and efficiency. Use when the user wants to model an antenna on a vehicle, aircraft, ship, satellite, or other large structure.
matlab-analyze-pcb-pdnPDN DC voltage/current analysis, IR drop, design rule checking, and multi-net batch analysis on imported PCB layouts. TRIGGER: user asks about power integrity, PDN analysis, IR drop, voltage distribution, current density, power nets, or design rule checking on a PCB. Invoke BEFORE writing code — the PDN API chain is specialized and non-obvious. SKIP: importing a PCB file (use matlab-read-pcb-layout), EM field/S-parameter extraction (use matlab-analyze-em), material/stackup setup only (use matlab-manage-pcb-material), transmission line design (use matlab-design-pcb-txline).
matlab-analyze-rcsCalculate and visualize monostatic and bistatic radar cross section (RCS) using MATLAB Antenna Toolbox. Computes RCS of platforms, antennas, and arrays with PO, MoM, and FMM solvers, supporting HH/VV/HV/VH polarization, GPU acceleration, and near-field observation. Use when the user wants to compute, plot, or analyze radar cross section.
matlab-analyze-rf-propagationAnalyze RF propagation and plan wireless sites using MATLAB Antenna Toolbox. Creates transmitter/receiver sites, computes signal strength, coverage maps, SINR, line-of-sight, and ray tracing in geographic or indoor environments. Supports multiple propagation models (free-space, close-in, Longley-Rice, ray tracing, rain/gas/fog), custom terrain, building data, and directional antennas. Use when the user wants to compute coverage, signal strength, path loss, SINR, ray tracing, or plan a wireless network.