Load NWB LFP, map macro-wire channels, align behavior, and attach MNI labels.
This script demonstrates a clean starting point for working with NWB-based intracranial LFP recordings.
It supports two acquisition modes (raw vs filt), extracts macrowire channels,
aligns LFP time to behavioral trial events, and loads a channel table containing MNI/Freesurfer labels.
! Version Lock
Use MATLAB 2024a with MatNWB 2.5.0 for compatibility. If your NWB schema differs, adjust object paths.
What this script does
OverviewNWB load (raw vs filtered)
Finds *.nwb files and selects either *raw* or *filter*. Loads timestamps + data from the appropriate NWB path.
Timebase handling
For filtered data, timestamps are downsampled by 8 to match 500 Hz. Raw data keeps the native 4 kHz timestamps.
Macro-wire channel selection
Pulls electrode labels and filters to MA_ channels. Also loads location, hemisphere, and a short brain-area name per channel.
Behavior alignment + trial masks
Loads a behavior MAT file with eventTABLE/behavInfo, then finds the LFP sample index for trial start.
Inputs / Expected folder layout
SetupThe script expects two working directories: one containing NWB files, and one containing behavior and MNI MAT files.
project/
├─ nwb/ # your NWB files
│ ├─ subject01_filter.nwb
│ ├─ subject01_raw.nwb
│ └─ ...
└─ behavior_mni/ # your MAT files
├─ *Behavior*.mat # contains eventTABLE, behavInfo
├─ *MNI*.mat # contains tmpCSV (channel table)
└─ ...
-
1
Point
nwbLOCto the directory containing*.nwb, thencdinto it. -
2
Set
toLOADto'filt'(500 Hz) or'raw'(4000 Hz). -
3
Set the behavior/MNI directory (the second
cd(...)) to your MAT location.
Usage
How-ToMinimal run sequence:
%% Requirements
% MATLAB 2024a + MatNWB 2.5.0
%% 1) NWB directory
nwbLOC = 'DIRECTORY FOR NWB FILES';
cd(nwbLOC)
%% 2) Choose what to load
toLOAD = 'filt'; % 'raw'
%% 3) Run the script sections:
% - NWB load
% - channel selection (MA_)
% - behavior load + alignment
% - MNI labels load
When to use toLOAD = 'raw'
You need high temporal resolution (4 kHz), want to run your own filtering, or need to match raw acquisition timing.
When to use toLOAD = 'filt'
You want a lighter dataset (500 Hz) already high-pass filtered (0.5 Hz) and notch-filtered.
Key variables created
OutputsLFP time + data
LFP_time (timestamps) and LFP_dataD (double voltage data).
Macro-wire channel metadata
MAchan, chanID2, chanHemi, chanSname.
Behavior and alignment
eventTABLE, behavInfo, sampleINDEX (start of Trial 1).
Trial masks
LA_trials, RA_trials, gamble_trials, plus outcome masks (loss/neutral/gain).
Notes & Common Pitfalls
Important-
!
Filtered timestamps: The script assumes the NWB timestamps were stored at 4 kHz and must be downsampled by 8 to match 500 Hz filtered data.
-
!
NWB paths differ: Filtered LFP is pulled from
processing/ecephys/LFP, while raw is pulled fromacquisition/MacroWireSeries. -
!
Naming consistency: Your snippet uses
checkIndexandeventTab. Ensure these match your MAT file variables.