Why do I get an error when creating an empty, zero-length HDF5 dataset with using "h5create" in MATLAB R2024b?

1 view (last 30 days)
I have a workflow where I need to always include certain datasets in my HDF5 files, even if they are empty for a particular run. This is important for maintaining a consistent file structure and for attaching attributes to these datasets for self-documentation. However, when I try to use "h5create" in MATLAB to create a fixed-size, zero-length dataset (e.g., [0 0]), I receive an error. Is there a way to create an empty HDF5 dataset with attributes in MATLAB?
Here are the steps I tried:
>> h5create("test.h5", "/0x0", [0, 0], Datatype="uint8", Chunksize=[0, 0])
Here is the error I receive:
Error using matlab.internal.sci.hdf5lib2
The HDF5 library encountered an error and produced the following stack trace information:
H5Pset_chunk all chunk dimensions must be positive

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Jan 2026
MATLAB’s high-level HDF5 interface ("h5create") does not currently support creating fixed-size, zero-length datasets directly. However, there are two approaches you can use to achieve similar functionality:
1. Create an Extendable (Unlimited) Dataset and leave it empty:
You can create an extendable dataset with unlimited dimensions and a nominal chunk size. If you do not write any data to it, the dataset will remain empty (size 0x0), but it will exist in the file and you can add data as needed.
Here is an example:
>> h5create('test.h5', '/empty', [Inf Inf], 'Datatype', 'double', 'ChunkSize', [1 1]);
>> h5writeatt('test.h5', '/empty', 'Description', 'This is an empty dataset');
This dataset will appear as 0x0 in the file until you write data to it.
2. Use the Low-Level HDF5 Interface to create a fixed-size, zero-length dataset:
If you specifically need a fixed-size, zero-length dataset (such as [0 0]), you can use MATLAB’s low-level HDF5 functions.
 Here is an example:
>> fid = H5F.create("myfile.h5");
>> typeID = H5T.copy("H5T_NATIVE_DOUBLE");
>> dims = [0 0];
>> spaceID = H5S.create_simple(numel(dims), dims, dims);
>> dsID = H5D.create(fid,"emptyDataset",typeID,spaceID,"H5P_DEFAULT");
>> H5D.close(dsID);
>> H5S.close(spaceID);
>> H5T.close(typeID);
>> H5F.close(fid);
This will create a fixed-size, zero-length dataset.
If none of these alternative workflows work for you, please reach out to MathWorks Technical Support for further help.

More Answers (0)

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!