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/wf-custom-readfcn.md

View on GitHub
# Workflow: custom `ReadFcn` (only when needed)

> Functions used: `signalDatastore`

End-to-end recipe for authoring a custom reader. Use only when the file
format is **not** `.mat` / `.csv`, or when there's a metadata prelude the
default reader can't parse. (`.wav` is not in the default-reader scope —
it requires `audioDatastore` or a custom `ReadFcn`.)

> **Off-ramp.** If your files are `.csv` or `.mat` with multiple variables,
> the default reader handles them via `SignalVariableNames` — no custom
> `ReadFcn` needed → see fn-signaldatastore.md.

## Decision tree before reaching for `ReadFcn`

1. **Is the file `.mat` or `.csv`?** Default reader probably works.
   (`.wav` is **not** in the default-reader scope.)
2. **Multi-column or named variables?** `SignalVariableNames` NV-pair.
3. **Sample rate in a column or variable?** `SampleRateVariableName` NV-pair.
4. **Only then** → custom `ReadFcn`.

If you're here because a reflex said "non-standard input → custom
function", reset and check the NV-pair list in fn-signaldatastore.md
first. Tabular CSV doesn't need a custom `ReadFcn`.

## Recipe

### Step 1 — Author the reader

```matlab
function [data, info] = myReader(filename)
    % Open file, parse header, read signal:
    fid = fopen(filename, "rb");             % binary mode
    header = fread(fid, 16, "uint8");        % example: 16-byte prelude
    fs     = double(typecast(uint8(header(1:8)), "double"));
    data   = fread(fid, Inf, "single");
    fclose(fid);

    % info surfaces per-file metadata to downstream consumers:
    info.SampleRate = fs;
end
```

If the reader has nothing to surface beyond the data itself, the 1-output
form is sufficient — see "Signature" below.

### Step 2 — Wire it into the datastore

```matlab
sds = signalDatastore(folder, ...
    FileExtensions=".dat", ...
    ReadFcn=@myReader);
```

### Step 3 — Verify

```matlab
[data, info] = read(sds);
disp(class(data))      % what your reader returned
disp(info.FileName)    % datastore appends this automatically
```

## Worked example — `.wav` without Audio Toolbox

When `audioDatastore` (Audio Toolbox) isn't available, `audioread`
(base MATLAB) is the canonical fallback for raw `.wav` ingestion:

```matlab
sds = signalDatastore(dataFolder, ...
    FileExtensions=".wav", ...
    ReadFcn=@readWav);

function [data, info] = readWav(filename)
    [data, fs] = audioread(filename);
    info.SampleRate = fs;
end
```

`audioread` is in base MATLAB, so this works without Audio Toolbox.
When Audio Toolbox is on the path, prefer `audioDatastore` — it
handles label-source overloads and streaming out of the box.

(Don't set `info.FileName` — the datastore appends it automatically.)

## Signature

Both forms are valid:

```matlab
function data = fn(filename)              % 1-output
function [data, info] = fn(filename)      % 2-output
```

Use the 2-output form **only when** the reader needs to attach per-file
metadata to `info` (e.g. `info.SampleRate` for variable-rate files).
Otherwise the 1-output form is cleaner.

`info.FileName` is appended by the datastore automatically; user-supplied
values are overwritten, so don't set it.

## Anti-pattern — eager-load

Don't `load(file)` inside the reader and discard most variables. At scale
this wastes memory proportional to total file size, not to the variable
you actually need. Use `load(file, "x")` with named variables, or
`matfile` for lazy access:

```matlab
% Bad:
function data = badReader(filename)
    s = load(filename);     % loads everything
    data = s.x;
end

% Good — named load:
function data = goodReader(filename)
    s = load(filename, "x");
    data = s.x;
end

% Better — matfile lazy access:
function data = lazyReader(filename)
    m = matfile(filename);
    data = m.x;
end
```

## Anti-pattern — wrapping `readtable` for tabular CSV

If your CSV has named columns and you want a subset, the default reader
does it directly:

```matlab
% Bad — custom ReadFcn for tabular CSV:
sds = signalDatastore(folder, ReadFcn=@(f) ...
    table2array(readtable(f, "SelectedVariableNames", ["ch1","ch2"])));

% Good — default reader, NV-pair:
sds = signalDatastore(folder, FileExtensions=".csv", ...
    SignalVariableNames=["ch1","ch2"]);
```

## Gotchas

- **Pick the signature deliberately.** 1-output if the reader has nothing
  to attach beyond the data; 2-output only when you need `info.SampleRate`
  or other per-file metadata.
- **Don't set `info.FileName`.** The datastore overwrites it.
- **`ReadFcn` is called once per file, not once per `read(sds)` call** —
  the datastore caches reads as it advances. Don't put per-batch state
  inside the reader.
- **`ReadFcn` runs on workers in parallel processing.** Don't capture
  workspace variables (closure) — pass them via the file or as
  reader-local constants.

## See also

- `fn-signaldatastore.md` — full NV-pair table of default-reader options.
- `wf-load-and-split.md` — the canonical workflow that *doesn't* need a
  custom `ReadFcn`.

----

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