Creating NWB Files

When creating an NWB file, you’re translating your experimental data and metadata into a structure that follows the NWB schema. MatNWB provides MATLAB classes that represent the different components (neurodata types) of an NWB file, allowing you to build up the file piece by piece.

Tip

To understand the general structure of an NWB file, the NWB Overview documentation has a great introduction.

As demonstrated in the Quickstart tutorial, when creating an NWB file, you start by invoking the NwbFile class. This will return an NwbFile object, a container whose properties are derived directly from the NWB schema. Some properties are required, others are optional. Some need specific MATLAB types like char or datetime, while others need specific neurodata types defined in the NWB schema.

Note

An “object” is an instance of a class. Objects are similar to MATLAB structs, but with additional functionality. The fields (called properties) are defined by the class definition (a .m file), and the class can enforce rules about what values are allowed. This helps ensure that your data conforms to the NWB schema.

General steps to create an NWB file

Building an NWB file follows a few general steps:

  • Create neurodata objects: Create neurodata type objects and add your relevant data and metadata (like types.core.TimeSeries for time-based measurements)

  • Add to containers: Add these neurodata type objects to your NwbFile object (or other NWB container objects) in appropriate locations

  • File export: Save everything to disk using nwbExport(), which translates your objects into NWB/HDF5 format

This approach ensures your data is properly organized and validated before it becomes a file.

Schema Validation

The NWB schema acts as a blueprint that defines what makes a valid NWB data file. When you export your file, MatNWB checks that:

  • All required properties are present

  • Data types match what the schema expects

  • Relationships between different parts of the file are correct

If anything is missing or incorrect, you’ll get an error message explaining what needs to be fixed. This validation helps ensure your files will work with other NWB tools and can be understood by other researchers.