Main Content

h5write

Write to HDF5 dataset

Description

example

h5write(filename,ds,data)writes data to an entire dataset,ds, in the specified HDF5 file. If the dataset is fixed in size, the amount of data to be written must match the size of the dataset.

example

h5write(filename,ds,data,start,count)writes a subset of data to a dataset, beginning at starting locationstart, and continuing forcountelements. In a multidimensional dataset,countspecifies a distance in each direction.h5writeextends an extendable dataset along any unlimited dimensions, if necessary.

example

h5write(filename,ds,data,start,count,stride)specifies the spacing between elements,stride, along each dimension of the dataset.

Examples

collapse all

Create a 10-by-20 dataset namedDS1.

h5create('myfile.h5','/DS1',[10 20])

Write a 10-by-20 array of random numbers to the dataset. Since the dimensions of'DS1'are fixed, the amount of data to be written to it must match its size.

mydata = rand(10,20); h5write('myfile.h5','/DS1', mydata)

Display the contents of the file.

h5disp('myfile.h5')
HDF5 myfile.h5 Group '/' Dataset 'DS1' Size: 10x20 MaxSize: 10x20 Datatype: H5T_IEEE_F64LE (double) ChunkSize: [] Filters: none FillValue: 0.000000

Create a 10-by-20 dataset namedDS2.

h5create('myfile.h5','/DS2',[10 20])

Write a 5-by-7 subset of data to the last 5-by-7 block of the dataset. Specifycountas[5 7]to match the size of the data you are writing. Specifystartas[6 14], because movingcountcells from this starting point will end in the last element of the dataset.

mydata = rand(5,7); h5write('myfile.h5','/DS2',mydata,[6 14],[5 7])

Write data to a dataset that has an unlimited dimension.

Create a dataset that is unlimited along the second dimension.ChunkSizemust be specified to set any dimension of the dataset toInf.

h5create('myfile.h5','/g2/DS2',[20 Inf],"Chunksize",[5 5]);

Write a 3-by-3 block of data to'/g2/DS2'. Begin at the starting point[3 2]and write to the end of the block. You can write data of any size along the second dimension of the dataset, since it is unlimited.

data = rand(3); start = [3 2]; count = [3 3]; h5write('myfile.h5','/g2/DS2',data,start,count);

Read all of the data from the dataset.

h5read('myfile.h5','/g2/DS2')
ans =20×40 0 0 0 0 0 0 0 0 0.8147 0.9134 0.2785 0 0.9058 0.6324 0.5469 0 0.1270 0.0975 0.9575 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ⋮

Input Arguments

collapse all

File name, specified as a character vector or string scalar containing the name of an existing HDF5 file.

Depending on the location you are writing to,filenamecan take on one of these forms.

Location

Form

Current folder

To write to the current folder, specify the name of the file infilename.

Example:'myFile.h5'

Other folders

To write to a folder different from the current folder, specify the full or relative path name infilename.

Example:'C:\myFolder\myFile.h5'

Example:'myFolder\myFile.h5'

Remote Location

To write to a remote location,filenamemust contain the full path of the file specified as a uniform resource locator (URL) of the form:

scheme_name://path_to_file/my_file.ext

Based on your remote location,scheme_namecan be one of the values in this table.

Remote Location scheme_name
Amazon S3™ s3
Windows Azure®Blob Storage wasb,wasbs

For more information, seeWork with Remote Data.

Example:'s3://bucketname/path_to_file/myFile.h5'

Dataset name, specified as a character vector or string scalar containing the name of an existing dataset in the HDF5 file.

Data to be written to the HDF5 file. If a numeric datatype was specified in the corresponding call toh5create, thendatais a numeric matrix containing floating-point or integer data. Data must be non sparse, and must be the same size as the HDF5 dataset if you do not specifystartorcount. If a dimension in the dataset is unlimited, then the data to be written can be any size along that dimension.

If'string'was specified as the datatype in the corresponding call toh5create,datais a MATLAB string array. The string array dimensions must match those specified in the call toh5create.

Starting location, specified as a numeric vector of positive integers. For an N-dimensional dataset,startis a vector of length N containing 1-based indices. The elements ofstartcorrespond, in order, to the dataset dimensions.

If you do not specifystart, then theh5writefunction starts writing to the dataset from the first index along each dimension.

Number of elements to write, specified as a numeric vector of positive integers. For an N-dimensional dataset,countis a vector of length N, specifying the number of elements to write to the dataset along each dimension. The elements ofcountcorrespond, in order, to the dataset dimensions.

Spacing between elements along each dimension of the dataset, specified as a numeric vector of integers. For an N-dimensional dataset,strideis a vector of length N. The elements of thestridevector correspond, in order, to the dataset dimensions. A value of1writes without skipping elements in the corresponding dimension, whereas, a value of2writes every other element, and so on.

If you do not specify stride, then theh5writefunction writes data without skipping along each dimension.

Limitations

  • h5writedoes not support writing to files stored remotely in HDFS™.

Version History

Introduced in R2011a

expand all

Behavior changed in R2020a