### MATLAB Usage Examples Source: https://matnwb.readthedocs.io/en/latest/pages/developer/documentation/formatting_docstrings.html Provide practical examples of function usage, each starting with 'Example X - Description::'. Include indented MATLAB code blocks with explanatory comments. ```matlab % Usage: % Example 1 - Read an NWB file:: % % nwb = nwbRead('data.nwb'); % % Example 2 - Read an NWB file without re-generating classes for NWB types:: % % nwb = nwbRead('data.nwb', 'ignorecache'); % % Note: This is a good option to use if you are reading several files % which are created of the same version of the NWB schemas. % % Example 3 - Read an NWB file and generate classes for NWB types in the current working directory:: % % nwb = nwbRead('data.nwb', 'savedir', '.'); ``` -------------------------------- ### Install MatNWB from Source Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/intro.html Use these commands to clone the MatNWB repository and add it to your MATLAB path. This is the recommended method for installation. ```matlab !git clone https://github.com/NeurodataWithoutBorders/matnwb.git addpath(genpath(pwd)); ``` -------------------------------- ### Retrieve All ElectricalSeries Objects Source: https://matnwb.readthedocs.io/en/latest/pages/functions/NwbFile.html This example shows how to get all objects of the 'ElectricalSeries' type from an NwbFile object. It requires running an initialization script 'ecephys.mlx' beforehand. ```matlab evalc('run("ecephys.mlx")'); nwb.getTypeObjects('ElectricalSeries') ``` -------------------------------- ### Install MatNWB from Source Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/read_demo.html Installs the MatNWB library from GitHub if it's not already on the MATLAB search path. Ensure Git is installed and accessible. ```matlab if ~exist('nwbRead', 'file') % Skip if MatNWB is on MATLAB's search path !git clone https://github.com/NeurodataWithoutBorders/matnwb.git % add the path to matnwb and generate the core classes addpath('matnwb'); end ``` -------------------------------- ### Install MatNWB from Source Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/intro.html Use these commands to clone the MatNWB repository and add it to your MATLAB path. This is the recommended way to install MatNWB. ```matlab !git clone https://github.com/NeurodataWithoutBorders/matnwb.git addpath(genpath(pwd)); ``` -------------------------------- ### Install hdf5plugin and Get Plugin Path (Windows) Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/dynamically_loaded_filters.html Install the hdf5plugin Python package and retrieve the plugin path. This path will be used to set the HDF5_PLUGIN_PATH environment variable. ```bash pip install hdf5plugin ``` ```bash python -c "import hdf5plugin; print(hdf5plugin.PLUGINS_PATH)" ``` -------------------------------- ### Quick Install MatNWB from GitHub Source: https://matnwb.readthedocs.io/en/latest/pages/getting_started/installation.html Clones MatNWB into the current directory, adds it to the MATLAB path, and optionally persists the change. Requires git. ```matlab !git clone https://github.com/NeurodataWithoutBorders/matnwb.git addpath("matnwb") % Optional: persist for future MATLAB sessions savepath() ``` -------------------------------- ### Setup Environment with Acquired Data Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/scratch.html Initializes an NWBFile with required metadata and simulates acquired data as a TimeSeries object. This sets up the context for demonstrating scratch space usage. ```matlab ContextFile = NwbFile(... 'session_description', 'demonstrate NWBFile scratch', ... % required 'identifier', 'SCRATCH-0', ... % required 'session_start_time', datetime(2019, 4, 3, 11, 0, 0, 'TimeZone', 'local'), ... % required 'file_create_date', datetime(2019, 4, 15, 12, 0, 0, 'TimeZone', 'local'), ... % optional 'general_experimenter', 'Niu, Lawrence', 'general_institution', 'NWB' ... ); % Simulate some data timestamps = 0:100:1024; data = sin(0.333 .* timestamps) ... + cos(0.1 .* timestamps) ... + randn(1, length(timestamps)); RawTs = types.core.TimeSeries(... 'data', data, ... 'data_unit', 'm', 'starting_time', 0., ... 'starting_time_rate', 100, 'description', 'simulated acquired data' ... ); ContextFile.acquisition.set('raw_timeseries', RawTs); % "Analyze" the simulated data % We provide a re-implementation of scipy.signal.correlate(..., mode='same') % Ideally, you should use MATLAB-native code though using its equivalent % function (xcorr) requires the Signal Processing Toolbox correlatedData = sameCorr(RawTs.data, ones(128, 1)) ./ 128; % If you are unsure of how HDF5 paths map to MatNWB property structures, we % suggest using HDFView to verify. In most cases, MatNWB properties map % directly to HDF5 paths. FilteredTs = types.core.TimeSeries( ... 'data', correlatedData, ... 'data_unit', 'm', 'starting_time', 0, 'starting_time_rate', 100, 'description', 'cross-correlated data' ... ) FilteredTs = TimeSeries with properties: starting_time_unit: 'seconds' timestamps_interval: 1 timestamps_unit: 'seconds' data: [-0.0062 -0.0062 -0.0062 -0.0062 -0.0062 -0.0062 -0.0062 -0.0062 -0.0062 -0.0062 -0.0062] data_unit: 'm' comments: 'no comments' control: [] control_description: '' data_continuity: '' data_conversion: 1 data_offset: 0 data_resolution: -1 description: 'cross-correlated data' starting_time: 0 starting_time_rate: 100 timestamps: [] ProcModule = types.core.ProcessingModule( ... 'description', 'a module to store filtering results', 'filtered_timeseries', FilteredTs ... ); ContextFile.processing.set('core', ProcModule); nwbExport(ContextFile, 'context_file.nwb'); ``` -------------------------------- ### Retrieve ElectricalSeries and Subtype Objects Source: https://matnwb.readthedocs.io/en/latest/pages/functions/NwbFile.html This example retrieves all 'ElectricalSeries' objects and any objects of its subtypes from an NwbFile. Ensure 'ecephys.mlx' is run prior to execution. ```matlab evalc('run("ecephys.mlx")') nwb.getTypeObjects('ElectricalSeries', 'IncludeSubTypes', true) ``` -------------------------------- ### Create a New NWB File Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/intro.html Initialize a new NWBFile object with required metadata such as session description, identifier, and start time. Optional fields can also be included. ```matlab nwb = NwbFile( 'session_description', 'mouse in open exploration', 'identifier', 'Mouse5_Day3', 'session_start_time', datetime(2018, 4, 25, 2, 30, 3, 'TimeZone', 'local'), 'general_experimenter', 'Last, First', ... % optional 'general_session_id', 'session_1234', ... % optional 'general_institution', 'University of My Institution', ... % optional 'general_related_publications', {'DOI:10.1016/j.neuron.2016.12.011'}); % optional nwb ``` -------------------------------- ### Create Minimal NWB File Object Source: https://matnwb.readthedocs.io/en/latest/pages/getting_started/quickstart.html Initializes an NwbFile object with required metadata: identifier, session description, and session start time. ```matlab nwb = NwbFile( 'identifier', 'quickstart-demo-20250411T153000Z', 'session_description', 'Quickstart demo session', 'session_start_time', datetime(2025,4,11,15,30,0,'TimeZone','UTC')); ``` -------------------------------- ### Create NWB File with Metadata Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/ophys.html Initializes a new NWBFile object with essential metadata like session description, identifier, and start time. Uses keyword argument pairs for configuration. ```matlab nwb = NwbFile( ... 'session_description', 'mouse in open exploration', 'identifier', 'Mouse5_Day3', ... 'session_start_time', datetime(2018, 4, 25, 2, 30, 3, 'TimeZone', 'local'), ... 'timestamps_reference_time', datetime(2018, 4, 25, 3, 0, 45, 'TimeZone', 'local'), ... 'general_experimenter', 'LastName, FirstName', ... % optional 'general_session_id', 'session_1234', ... % optional 'general_institution', 'University of My Institution', ... % optional 'general_related_publications', {'DOI:10.1016/j.neuron.2016.12.011'}); % optional nwb ``` -------------------------------- ### Determine Plugin Path (Windows) Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/dynamically_loaded_filters.html Determine the installation path of the hdf5plugin by running a Python command in the Command Prompt. This path is needed to set the environment variable. ```bash python -c "import hdf5plugin; print(hdf5plugin.PLUGINS_PATH)" ``` -------------------------------- ### Initialize Scratch NWBFile and Read Context File Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/scratch.html Creates a new NWBFile for scratch data and reads an existing NWB file containing processed data. Essential metadata is copied from the context file to the scratch file. ```matlab ScratchFile = NwbFile('identifier', 'SCRATCH-1'); ContextFile = nwbRead('./context_file.nwb', 'ignorecache'); % again, copy the required metadata from the processed file. ScratchFile.session_description = ContextFile.session_description; ScratchFile.session_start_time = ContextFile.session_start_time; ``` -------------------------------- ### Verify MatNWB Installation Source: https://matnwb.readthedocs.io/en/latest/pages/getting_started/installation.html Checks if MatNWB is installed by retrieving its version information. If the folder name differs, update 'matnwb' accordingly. ```matlab versionInfo = ver("matnwb") ``` -------------------------------- ### Install NWB Extension Source: https://matnwb.readthedocs.io/en/latest/pages/functions/nwbInstallExtension.html Installs a specified NWB extension by its name. The extension name must be a valid name from the Neurodata Extensions Catalog. ```APIDOC ## nwbInstallExtension ### Description Installs a specified NWB extension to extend the functionality of the core NWB schemas. Accepts a scalar string or a string array of extension names. ### Syntax nwbInstallExtension(_extensionNames_ , _options_) ### Parameters #### Path Parameters - **extensionNames** (string or string array) - Required - The name(s) of the NWB extension(s) to install from the Neurodata Extensions Catalog. #### Query Parameters - **options** (object) - Optional - Additional options for installation (details not provided in source). ### Usage **Example 1 - Install “ndx-miniscope” extension** : ```matlab nwbInstallExtension("ndx-miniscope") ``` ### See also `matnwb.extension.listExtensions()`, `matnwb.extension.installExtension()` ``` -------------------------------- ### Initialize NWBFile with Required Metadata Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/scratch.html Initializes an NWBFile object with essential session information. Ensure 'session_description', 'identifier', and 'session_start_time' are provided. ```matlab ContextFile = NwbFile(... 'session_description', 'demonstrate NWBFile scratch', ... % required 'identifier', 'SCRATCH-0', ... % required 'session_start_time', datetime(2019, 4, 3, 11, 0, 0, 'TimeZone', 'local'), ... % required 'file_create_date', datetime(2019, 4, 15, 12, 0, 0, 'TimeZone', 'local'), ... % optional 'general_experimenter', 'Niu, Lawrence', 'general_institution', 'NWB' ... ); ``` -------------------------------- ### Install hdf5plugin (Linux/Mac) Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/dynamically_loaded_filters.html Install the hdf5plugin Python package using pip. This package helps manage dynamically loaded filters for HDF5. ```bash pip install hdf5plugin ``` -------------------------------- ### Install a Published Neurodata Extension Source: https://matnwb.readthedocs.io/en/latest/pages/how_to/using_extensions/installing_extensions.html Use this function to download and generate classes for a specific published Neurodata Extension. Replace 'ndx-extension' with the actual name of the extension you want to install. ```python nwbInstallExtension("ndx-extension") ``` -------------------------------- ### Install hdf5plugin and Set Environment Variable (Linux/Mac) Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/dynamically_loaded_filters.html Install the hdf5plugin Python package and set the HDF5_PLUGIN_PATH environment variable in your terminal. This is necessary to enable dynamically loaded filters in MATLAB. ```bash pip install hdf5plugin ``` ```bash export HDF5_PLUGIN_PATH=$(python -c "import hdf5plugin; print(hdf5plugin.PLUGINS_PATH)") ``` -------------------------------- ### Create NWB File with Session Metadata Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/behavior.html Initialize an NWBFile object with essential session details and optional metadata. Ensure 'session_description', 'identifier', and 'session_start_time' are provided. ```matlab nwb = NwbFile( ... 'session_description', 'mouse in open exploration', 'identifier', 'Mouse5_Day3', 'session_start_time', datetime(2018, 4, 25, 2, 30, 3, 'TimeZone', 'local'), 'general_experimenter', 'My Name', ... % optional 'general_session_id', 'session_1234', ... % optional 'general_institution', 'University of My Institution', ... % optional 'general_related_publications', 'DOI:10.1016/j.neuron.2016.12.011'); % optional ``` -------------------------------- ### Create and Add OptogeneticStimulusSite Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/ogen.html Create a Device object, link it to the NWBFile, and then create and add an OptogeneticStimulusSite. This site contains metadata about the stimulus location and properties. ```python device = types.core.Device() nwb.general_devices.set('Device', device) ogen_stim_site = types.core.OptogeneticStimulusSite( 'device', types.untyped.SoftLink(device), 'description', 'This is an example optogenetic site.', 'excitation_lambda', 600.0, 'location', 'VISrl') nwb.general_optogenetics.set('OptogeneticStimulusSite', ogen_stim_site) ``` -------------------------------- ### Install a Single NWB Extension Source: https://matnwb.readthedocs.io/en/latest/pages/functions/nwbInstallExtension.html Use this function to install a single NWB extension by providing its name as a string. Ensure the extension name is valid and available in the Neurodata Extensions Catalog. ```matlab nwbInstallExtension("ndx-miniscope") ``` -------------------------------- ### Store Running Intervals Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/behavior.html Use IntervalSeries within BehavioralEpochs to store time intervals, marking the start and end of an activity. Data values of 1 and -1 indicate interval start and end, respectively. ```matlab run_intervals = types.core.IntervalSeries( ... 'description', 'Intervals when the animal was running.', ... 'data', [1, -1, 1, -1, 1, -1], ... 'timestamps', [0.5, 1.5, 3.5, 4.0, 7.0, 7.3] ... ); behavioral_epochs = types.core.BehavioralEpochs(); behavioral_epochs.intervalseries.set('running', run_intervals); ``` -------------------------------- ### IZeroClampSeries Data Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/icephys.html Example of creating an IZeroClampSeries object for recordings where current is clamped to 0. Ensure 'electrode' is a valid object. ```matlab % Create an IZeroClampSeries object izcs = types.core.IZeroClampSeries(... 'data', [0.1, 0.2, 0.3, 0.4, 0.5], ... 'electrode', types.untyped.SoftLink(electrode), ... 'gain', 0.02, ... 'data_conversion', 1e-12, ... 'data_resolution', NaN, ... 'starting_time', 345.6, ... 'starting_time_rate', 20e3, ... 'sweep_number', uint64(17) ... ); nwbfile.acquisition.set('IZeroClampSeries', izcs); ``` -------------------------------- ### Create NWBFile Object Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/behavior.html Initialize an NWBFile object with required fields and optional metadata. Ensure session_start_time is a datetime object with timezone information. ```matlab nwb = NwbFile( ... 'session_description', 'mouse in open exploration', 'identifier', 'Mouse5_Day3', 'session_start_time', datetime(2018, 4, 25, 2, 30, 3, 'TimeZone', 'local'), ... 'general_experimenter', 'My Name', ... % optional 'general_session_id', 'session_1234', ... % optional 'general_institution', 'University of My Institution', ... % optional 'general_related_publications', 'DOI:10.1016/j.neuron.2016.12.011'); % optional nwb ``` -------------------------------- ### Create and Add ProcessingModule Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/intro.html Organize processed data into ProcessingModules. This example creates a 'behavior' module and adds a 'Position' object to it. ```matlab % create processing module behavior_module = types.core.ProcessingModule('description', 'contains behavioral data'); % add the Position object (that holds the SpatialSeries object) to the module % and name the Position object "Position" behavior_module.nwbdatainterface.set('Position', position); % add the processing module to the NWBFile object, and name the processing module "behavior" nwb.processing.set('behavior', behavior_module); ``` -------------------------------- ### Create NWB with Default Chunking Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/dataPipe.html Demonstrates creating an NWB file with a DataPipe using default chunking. This is useful for understanding the baseline performance before manual optimization. ```matlab fData = randi(250, 100, 1000); % Create fake data % create an nwb structure with required fields nwb = NwbFile( ... 'session_start_time', datetime('2020-01-01 00:00:00', 'TimeZone', 'local'), ... 'identifier', 'ident1', 'session_description', 'DataPipeTutorial'); fData_compressed = types.untyped.DataPipe('data', fData); fdataNWB=types.core.TimeSeries( ... 'data', fData_compressed, 'data_unit', 'mV', 'starting_time', 0.0, 'starting_time_rate', 30.0); nwb.acquisition.set('data', fdataNWB); nwbExport(nwb, 'DefaultChunks.nwb'); ``` -------------------------------- ### Get Spike Times for a Unit Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/ecephys.html Retrieve the spike times for a specific unit from the NWB file using the getRow method. ```matlab % You can use the getRow method of the table to load spike times of a specific unit. % To get the values, unpack from the returned table. nwb.units.getRow(1).spike_times{1} ``` -------------------------------- ### Inspect an NWB file Source: https://matnwb.readthedocs.io/en/latest/pages/functions/inspectNwbFile.html Use this snippet to run nwbinspector on an NWB file and get a basic report of potential issues. ```matlab report = inspectNwbFile('my_nwb_file.nwb'); ``` -------------------------------- ### Create and Link Device for Optogenetics Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/ogen.html Create a Device object and link it to the NWBFile. This is a prerequisite for adding optogenetic stimulus sites. ```python device = types.core.Device() nwb.general_devices.set('Device', device) ``` -------------------------------- ### Add IntracellularResponsesTable Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/icephys.html Define the IntracellularResponsesTable to store recorded response data, linking to TimeSeries objects with their respective start indices and counts. ```matlab ic_rec_table.responses = types.core.IntracellularResponsesTable( ... 'description', 'Table for storing intracellular response related metadata.', ... 'colnames', {'response'}, ... 'id', types.hdmf_common.ElementIdentifiers( ... 'data', int64([0; 1; 2]) ... ), 'response', types.core.TimeSeriesReferenceVectorData( ... 'description', ['Column storing the reference to the recorded response ', 'for the recording (rows)'], ... 'data', struct( ... 'idx_start', [0, 2, 0], 'count', [5, 3, 5], 'timeseries', [ ... types.untyped.ObjectView(vcs), ... % Voltage clamp response types.untyped.ObjectView(ccs), ... % Current clamp response types.untyped.ObjectView(izcs) ... % Current clamp response when current is off ] ) ) ); ``` -------------------------------- ### Read and Display NWB File Contents Source: https://matnwb.readthedocs.io/en/latest/pages/concepts/file_read/nwbfile.html This snippet demonstrates how to run a tutorial script to generate an NWB file and then read it using `nwbRead`. The `disp(nwb)` command shows the structure and properties of the loaded NwbFile object. ```matlab evalc("run('tutorials/ecephys.mlx')"); % Run tutorial with suppressed output nwb = nwbRead('tutorials/ecephys_tutorial.nwb'); disp(nwb) ``` -------------------------------- ### Access Specific Dataset from Acquisition Set Source: https://matnwb.readthedocs.io/en/latest/pages/concepts/file_read/nwbfile.html Retrieve a specific dataset, such as 'ElectricalSeries', from the acquisition Set object using the `get()` method. ```matlab >> disp(nwb.acquisition.get('ElectricalSeries')); ``` -------------------------------- ### Get NWB Schema Version Source: https://matnwb.readthedocs.io/en/latest/pages/concepts/file_read/schemas_and_generation.html Retrieves the schema version used to create an NWB file. Ensure the file path is correct. ```matlab version = util.getSchemaVersion('path/to/file.nwb'); fprintf('File uses NWB schema version: %s ', version); ``` -------------------------------- ### Create NWBFile Object Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/icephys.html Initializes an NWBFile object with essential session metadata. Ensure the session_start_time is timezone-aware. ```python session_start_time = datetime(2018, 3, 1, 12, 0, 0, 'TimeZone', 'local'); nwbfile = NwbFile( 'session_description', 'my first synthetic recording', 'identifier', 'EXAMPLE_ID', 'session_start_time', session_start_time, 'general_experimenter', 'Dr. Bilbo Baggins', 'general_lab', 'Bag End Laboratory', 'general_institution', 'University of Middle Earth at the Shire', 'general_experiment_description', ['I went on an adventure with thirteen dwarves ', 'to reclaim vast treasures.'], 'general_session_id', 'LONELYMTN' ); ``` -------------------------------- ### Create NWBFile Object with Metadata Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/ophys.html Initialize an NWBFile object with essential session metadata. Arguments are passed as name-value pairs. ```matlab nwb = NwbFile( 'session_description', 'mouse in open exploration', 'identifier', 'Mouse5_Day3', 'session_start_time', datetime(2018, 4, 25, 2, 30, 3, 'TimeZone', 'local'), 'timestamps_reference_time', datetime(2018, 4, 25, 3, 0, 45, 'TimeZone', 'local'), 'general_experimenter', 'LastName, FirstName', ... % optional 'general_session_id', 'session_1234', ... % optional 'general_institution', 'University of My Institution', ... % optional 'general_related_publications', {'DOI:10.1016/j.neuron.2016.12.011'}); % optional nwb ``` -------------------------------- ### Get Keys from Analysis Set Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/basicUsage.html Retrieves all property keys from the 'analysis' property of an NWB file, similar to MATLAB's containers.Map. ```matlab unit_names = keys(nwb.analysis); ``` -------------------------------- ### Construct and Export a Simple NwbFile Object Source: https://matnwb.readthedocs.io/en/latest/pages/functions/NwbFile.html Demonstrates how to create a basic NwbFile object, assign an Epochs object to it, and then export it to an NWB file. Ensure the 'types.core.Epochs' is available in your MATLAB path. ```matlab nwb = NwbFile; nwb.epochs = types.core.Epochs; nwbExport(nwb, 'epoch.nwb'); ``` -------------------------------- ### Accessing Stimulus Image Data Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/read_demo.html Get the data attribute from the StimulusPresentation OpticalSeries. This returns a DataStub, which is a lazy representation of the image data. ```matlab StimulusImageData = nwb.stimulus_presentation.get('StimulusPresentation').data ``` -------------------------------- ### Update MatNWB via Git Source: https://matnwb.readthedocs.io/en/latest/pages/getting_started/installation.html Updates the MatNWB installation by pulling the latest changes from the Git repository. Navigate to the MatNWB directory first. ```bash cd path/to/matnwb !git pull ``` -------------------------------- ### NWBREAD Function Documentation Source: https://matnwb.readthedocs.io/en/latest/pages/developer/documentation/formatting_docstrings.html This section details the NWBREAD function, explaining its syntax, input arguments (filename, flags, name-value pairs), and output arguments. It also provides usage examples for reading NWB files with different options. ```matlab % NWBREAD - Read an NWB file. % % Syntax: % nwb = NWBREAD(filename) Reads the nwb file at filename and returns an % NWBFile object representing its contents. % % nwb = NWBREAD(filename, flags) Reads the nwb file using optional % flags controlling the mode for how to read the file. See input % arguments for a list of available flags. % % nwb = NWBREAD(filename, Name, Value) Reads the nwb file using optional % name-value pairs controlling options for how to read the file. % % Input Arguments: % - filename (string) - % Filepath pointing to an NWB file. % % - flags (string) - % Flag for setting the mode for the NWBREAD operation. Available options are: % 'ignorecache'. If the 'ignorecache' flag is used, classes for NWB data types % are not re-generated based on the embedded schemas in the file. % % - options (name-value pairs) - % Optional name-value pairs. Available options: % % - savedir (string) - % A folder to save generated classes for NWB types. % % Output Arguments: % - nwb (NwbFile) - Nwb file object % % Usage: % Example 1 - Read an NWB file:: % % nwb = nwbRead('data.nwb'); % % Example 2 - Read an NWB file without re-generating classes for NWB types:: % % nwb = nwbRead('data.nwb', 'ignorecache'); % % Note: This is a good option to use if you are reading several files % which are created of the same version of the NWB schemas. % % Example 3 - Read an NWB file and generate classes for NWB types in the current working directory:: % % nwb = nwbRead('data.nwb', 'savedir', '.'); % % See also: % generateCore, generateExtension, NwbFile, nwbExport ``` -------------------------------- ### Compress and Add Timeseries Data to NWB Source: https://matnwb.readthedocs.io/en/latest/pages/tutorials/dataPipe.html This example demonstrates how to create fake data, compress it using DataPipe with specified compression level and chunk size, and then assign it to a TimeSeries object within an NWB file. It also shows how to export the NWB file. ```matlab fData=randi(250, 1, 10000); %assign data without compression nwb=NwbFile( 'session_start_time', datetime(2020, 1, 1, 0, 0, 0, 'TimeZone', 'local'), 'identifier','ident1', 'session_description', 'DataPipeTutorial'); ephys_module = types.core.ProcessingModule( 'description', 'holds processed ephys data'); nwb.processing.set('ephys', ephys_module); % compress the data fData_compressed=types.untyped.DataPipe( 'data', fData, 'compressionLevel', 3, 'chunkSize', [100 1], 'axis', 1); % Assign the data to appropriate module and write the NWB file fdataNWB=types.core.TimeSeries( 'data', fData_compressed, 'data_unit', 'mV', 'starting_time', 0.0, 'starting_time_rate', 30.0); ephys_module.nwbdatainterface.set('data', fdataNWB); nwb.processing.set('ephys', ephys_module); % write the file nwbExport(nwb, 'Compressed.nwb'); ``` -------------------------------- ### Set HDF5_PLUGIN_PATH (Linux/Mac) Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/dynamically_loaded_filters.html Set the HDF5_PLUGIN_PATH environment variable to the location of the installed hdf5plugin filters. This allows MATLAB to find and use the filters. ```bash export HDF5_PLUGIN_PATH=$(python -c "import hdf5plugin; print(hdf5plugin.PLUGINS_PATH)") ``` -------------------------------- ### Create NWBFile Object Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/ogen.html Initialize an NWBFile object with essential session metadata. This is the first step when creating a new NWB file. ```matlab nwb = NwbFile( ... 'session_description', 'mouse in open exploration', 'identifier', char(java.util.UUID.randomUUID), 'session_start_time', datetime(2018, 4, 25, 2, 30, 3, 'TimeZone', 'local'), 'general_experimenter', 'Last, First M.', ... % optional 'general_session_id', 'session_1234', ... % optional 'general_institution', 'University of My Institution', ... % optional 'general_related_publications', 'DOI:10.1016/j.neuron.2016.12.011'); % optional nwb ``` -------------------------------- ### Create NWB with Default Chunking Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/dataPipe.html Demonstrates creating an NWB file with a TimeSeries using DataPipe's default chunking. This is useful for understanding the baseline performance before manual optimization. ```matlab fData = randi(250, 100, 1000); % create an nwb structure with required fields nwb = NwbFile( 'session_start_time', datetime('2020-01-01 00:00:00', 'TimeZone', 'local'), 'identifier', 'ident1', 'session_description', 'DataPipeTutorial'); fData_compressed = types.untyped.DataPipe('data', fData); fdataNWB=types.core.TimeSeries( 'data', fData_compressed, 'data_unit', 'mV', 'starting_time', 0.0, 'starting_time_rate', 30.0); nwb.acquisition.set('data', fdataNWB); nwbExport(nwb, 'DefaultChunks.nwb'); ``` -------------------------------- ### Create Regularly Sampled TimeSeries Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/intro.html Use TimeSeries for data sampled at regular intervals. Specify the starting time and sampling rate for efficiency. ```matlab time_series_with_rate = types.core.TimeSeries( ... 'description', 'an example time series', ... 'data', linspace(0, 100, 10), ... 'data_unit', 'm', ... 'starting_time', 0.0, ... 'starting_time_rate', 1.0); ``` -------------------------------- ### Current Clamp Stimulus and Response Data Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/icephys.html Example of creating and setting CurrentClampStimulusSeries and CurrentClampSeries for a current clamp recording. Ensure 'electrode' is a valid object. ```matlab % Create a CurrentClampStimulusSeries object ccss = types.core.CurrentClampStimulusSeries(... 'data', [1, 2, 3, 4, 5], ... 'starting_time', 123.6, ... 'starting_time_rate', 10e3, ... 'electrode', types.untyped.SoftLink(electrode), ... 'gain', 0.02, ... 'sweep_number', uint64(16), ... 'stimulus_description', 'N/A' ... ); nwbfile.stimulus_presentation.set('CurrentClampStimulusSeries', ccss); % Create a CurrentClampSeries object ccs = types.core.CurrentClampSeries(... 'data', [0.1, 0.2, 0.3, 0.4, 0.5], ... 'data_conversion', 1e-12, ... 'data_resolution', NaN, ... 'starting_time', 123.6, ... 'starting_time_rate', 20e3, ... 'electrode', types.untyped.SoftLink(electrode), ... 'gain', 0.02, ... 'bias_current', 1e-12, ... 'bridge_balance', 70e6, ... 'capacitance_compensation', 1e-12, ... 'stimulus_description', 'N/A', ... 'sweep_number', uint64(16) ... ); nwbfile.acquisition.set('CurrentClampSeries', ccs); ``` -------------------------------- ### Create NWB File Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/images.html Initializes a new NWB file with essential session metadata. Ensure timestamps are correctly set with timezone information. ```matlab nwb = NwbFile( ... 'session_description', 'mouse in open exploration', 'identifier', 'Mouse5_Day3', 'session_start_time', datetime(2018, 4, 25, 2, 30, 3, 'TimeZone', 'local'), ... 'timestamps_reference_time', datetime(2018, 4, 25, 3, 0, 45, 'TimeZone', 'local'), ... 'general_experimenter', 'LastName, FirstName', ... % optional 'general_session_id', 'session_1234', ... % optional 'general_institution', 'University of My Institution', ... % optional 'general_related_publications', 'DOI:10.1016/j.neuron.2016.12.011' ... % optional ); ``` -------------------------------- ### Create and Export NWB File Source: https://matnwb.readthedocs.io/en/latest/_static/html/tutorials/dimensionMapNoDataPipes.html Instantiate an NwbFile object with essential session information and assign the created TimeIntervals table to the 'intervals_trials' property. Finally, export the NWB file. ```matlab % Create NwbFile object with required arguments file = NwbFile( ... 'session_start_time', datetime('2022-01-01 00:00:00', 'TimeZone', 'local'), ... 'identifier', 'ident1', 'session_description', 'test file' ... ); % Assign to intervals_trials file.intervals_trials = trials_table; % Export nwbExport(file, 'testFileNoDataPipes.nwb'); ```