Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/signal-processing/matlab-prepare-signal-data

matlab-prepare-signal-data

Verified

Use this skill when conditioning, loading, preparing, or labeling signal data for analysis or ML training. Covers: cleaning a single signal (fill gaps, remove drift, deoutlier, denoise, resample/align a time base) BEFORE analysis; building a `signalDatastore` pipeline; creating a `labeledSignalSet` for Signal Labeler; deriving labels (filename, folder, in-file, ROI, time-frequency ROI); stratified train/val/test splits; framing long signals; parallel processing; and shaping datastore output for `trainnet`. Triggers include "clean up this signal", "remove drift / detrend", "fill gaps", "remove spikes / outliers", "denoise", "resample to a uniform rate", "align channels", "labels from filenames", "stratified split", "prepare for Signal Labeler", and function names like `fillgaps`, `fillmissing`, `detrend`, `filloutliers`, `smoothdata`, `resample`, `synchronize`, `signalDatastore`, `labeledSignalSet`, `filenames2labels`, `folders2labels`, `splitlabels`, `framesig`, `framelbl`, `createDatastores`.

FileKindSize
SKILL.mdskill md9.5 KB
manifest.yamlconfig650 B
references/fn-countlabels.mdmarkdown2.1 KB
references/fn-detrend.mdmarkdown4.6 KB
references/fn-filenames2labels.mdmarkdown4.8 KB
references/fn-fillgaps.mdmarkdown3.3 KB
references/fn-fillmissing.mdmarkdown4.2 KB
references/fn-filloutliers.mdmarkdown5.0 KB
references/fn-folders2labels.mdmarkdown3.4 KB
references/fn-framelbl.mdmarkdown7.1 KB
references/fn-framesig.mdmarkdown4.1 KB
references/fn-hampel.mdmarkdown3.3 KB
references/fn-labeledsignalset.mdmarkdown9.1 KB
references/fn-labelspectrogramoptions.mdmarkdown4.6 KB
references/fn-medfilt1.mdmarkdown3.3 KB
references/fn-partition.mdmarkdown4.6 KB
references/fn-resample.mdmarkdown4.6 KB
references/fn-retime.mdmarkdown4.7 KB
references/fn-sgolayfilt.mdmarkdown3.5 KB
references/fn-signaldatastore.mdmarkdown11.9 KB
references/fn-signallabeldefinition.mdmarkdown5.9 KB
references/fn-signalmask-getmask.mdmarkdown8.6 KB
references/fn-smoothdata.mdmarkdown4.8 KB
references/fn-splitlabels.mdmarkdown5.3 KB
references/fn-subset.mdmarkdown3.9 KB
references/fn-synchronize.mdmarkdown4.7 KB
references/fn-wdenoise.mdmarkdown5.2 KB
references/wf-align-channels.mdmarkdown6.6 KB
references/wf-custom-readfcn.mdmarkdown5.0 KB
references/wf-denoise.mdmarkdown2.6 KB
references/wf-detrend-smooth-deoutlier.mdmarkdown6.6 KB
references/wf-envelope.mdmarkdown5.3 KB
references/wf-frame-and-label.mdmarkdown4.7 KB
references/wf-handoff-to-dl.mdmarkdown9.1 KB
references/wf-label-and-export.mdmarkdown8.7 KB
references/wf-load-and-split.mdmarkdown4.6 KB
references/wf-parallel-process.mdmarkdown4.8 KB
references/wf-repair-missing.mdmarkdown6.7 KB
references/wf-uniform-rate.mdmarkdown4.2 KB

references/fn-splitlabels.md

View on GitHub
# Function: `splitlabels`

> Used in: wf-load-and-split.md, wf-frame-and-label.md
> Toolbox: Signal Processing Toolbox

Stratified train/val/test splitter for a label source. Produces **index
sets** that you feed into `subset(ds, idx)` — does not slice the datastore
itself.

## Signature

```matlab
splitIndices = splitlabels(lblsrc, p)                    % stratified (default)
splitIndices = splitlabels(lblsrc, p, 'randomized')      % per-class shuffle
splitIndices = splitlabels(___, Name=Value)              % filters / table column
```

`lblsrc` accepts:
- **categorical / string / logical / numeric vector**
- **cell array** of character vectors / numeric / logical scalars
- **table** — set `TableVariable` to pick the column
- **datastore** whose `readall` returns one of the above
- **`CombinedDatastore`** — set `UnderlyingDatastoreIndex` to pick the constituent

`p` — proportions or counts. Vector of fractions summing to ≤ 1
(e.g. `[0.7 0.15 0.15]`), vector of integers (e.g. `[500 300]`), or
scalar in (0,1) for a single-cut.

> **There is no `'stratified'` literal flag.** `splitlabels` only accepts
> `'randomized'`. The default form (no flag) IS stratified — class
> proportions are preserved exactly in each bin. The `'randomized'` flag
> is also stratified but does an additional per-class shuffle so
> within-class order is randomized.

## Return shape — the most-replicated confusion point

`splitIndices` is **always an `(N+1)`-element cell array of index vectors**,
where N is the number of split ratios. The last cell holds whatever
fraction is left over (`1 - sum(ratios)`) and is **empty when
`sum(ratios) == 1`** — but it's still there.

For `ratios = [0.7 0.15 0.15]` → `splitIndices` is a `4×1` cell:
- `splitIndices{1}` — indices for the 70% bin (train)
- `splitIndices{2}` — indices for the 15% bin (val)
- `splitIndices{3}` — indices for the 15% bin (test)
- `splitIndices{4}` — empty (leftover; sum was 1.0)

```matlab
splitIndices = splitlabels(labels, [0.7 0.15 0.15]);
disp(class(splitIndices))           % cell
disp(size(splitIndices))            % [4 1]
disp(class(splitIndices{1}))        % double  — index vector into labels
disp(isempty(splitIndices{4}))      % true    — leftover bin is empty
```

Index the bins you asked for (`{1}`, `{2}`, `{3}`); ignore `{end}`.

## Canonical pattern

```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});
```

## Anti-pattern

Don't use `cvpartition` for datastore-backed splits. `cvpartition` is the
Stats/ML default but requires materializing labels first and doesn't
compose with `subset(ds, idx)`.

```matlab
% Bad — Stats/ML reflex, doesn't compose with datastores:
c = cvpartition(labels, "HoldOut", 0.3);
trainLabels = labels(training(c));   % labels are split, but datastore isn't
% ... now you need to manually map back to sds indices.

% Good — splitlabels returns indices ready for subset:
splitIndices = splitlabels(labels, [0.7 0.15 0.15]);
sdsTrain = subset(sds, splitIndices{1});
```

## Stratification

`splitlabels` stratifies by default — class proportions in each split
match class proportions in the input. If a class has too few samples for
the requested ratios, `splitlabels` errors with a clear message.

```matlab
% Check distribution before splitting:
countlabels(labels)
splitIndices = splitlabels(labels, [0.7 0.15 0.15]);   % errors if infeasible
```

## Name-value arguments (all 4)

| NV-pair | Default | Purpose |
|---|---|---|
| `Include` | all categories | Vector or cell array of categories to include in the split. Categories must match those in `lblsrc`. |
| `Exclude` | none | Vector or cell array of categories to drop before splitting. |
| `TableVariable` | first table variable | Column to use when `lblsrc` is a table. Character vector or string scalar. |
| `UnderlyingDatastoreIndex` | required for `CombinedDatastore` | Integer scalar selecting which underlying datastore in a `CombinedDatastore` carries the labels. |

```matlab
% Filter a noisy label set and split only A/B/C:
splitIndices = splitlabels(labels, [0.7 0.15 0.15], ...
    Include=["A","B","C"]);

% Table input — pick the label column:
splitIndices = splitlabels(T, [0.7 0.15 0.15], TableVariable="ClassName");
```

## Gotchas

- **Output is cell, not vector.** The single most-replicated confusion.
  `subset(sds, splitIndices)` (without `{}`) errors.
- **Index space is 1..numel(labels).** Make sure `labels` and `sds.Files`
  are aligned — derive `labels` from the same `sds` that you'll subset.
- **Class proportions must support the split.** If a class has 2 samples
  and you ask for `[0.7 0.15 0.15]`, no integer split satisfies the ratios
  for that class. The function errors clearly.
- **Order within each split is randomized.** If you need reproducibility,
  set `rng(seed)` before the call.
- **`'stratified'` is not a valid third positional argument** — only
  `'randomized'` is accepted. Stratification is the default behavior, not
  an opt-in flag.

## See also

- `fn-subset.md` — consumes the returned cell entries.
- `fn-countlabels.md` — pre-flight distribution check.
- For the chain `splitlabels` → `subset`, see wf-load-and-split.md (step 4).

----

Copyright 2026 The MathWorks, Inc.

----
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall