Honeybear
Honeybear

Reputation: 3138

Append data to hdf5 container with Matlab

I created a HDF5 container using MATLAB. Content are several images with dimensions [1024 1024 4] (= width, height, channels). I created and filled the dataset like this:

% GENERATE DATA
img = double(ones([1024 1024 4]));             % example for image
imgs_dset = double(ones([size(img) n_imgs]);   % dataset [1024 1024 4 n_imgs]
datasetname = '/data';

% CREATE hdf5 container
% dset_size is 'Inf' so that container can hold arbitrary number of imgs
dset_size = [size(img) Inf];                   % = [1024 1024 4 Inf]
h5create(filename, datasetname, dset_size, 'Datatype', class(imgs_dset));

% FILL dataset
h5write(filename, datasetname, imgs_dset);

That is all good, all n_imgs are written to the file. However I now want to add further images into the same dataset, not knowing the size n_imgs of the created file (the appending script accesses the file independently from the creation script).

For older versions there was an option like 'WriteMode', 'append', but now the function h5write expects the parameters start and count (doc is here).

I'm puzzled on how to determine these parameters. In the example they set

start = [1 j];  % probably translates to [1 n_imgs]?
count = [20 1]; % translates to [img ?]

How would I set these parameters in my example?

  1. If I know n_imgs?
  2. If I don't know n_imgs?

Upvotes: 0

Views: 606

Answers (1)

Jay Brady
Jay Brady

Reputation: 106

start is where in the data you are going to start writing to the file. count is how much data you will write to the file.

Assuming n_imgs have already been written to the file and you wish to append, start would be [1 1 1 n_imgs+1]. (i.e. start writing to the file at the beginning of where the next image will be). Say you wish to append 5 new images to the file, count will be [1024 1024 4 5].

If you don't know how many images have been written to the hdf5 previously, you can look it up using h5info

Here's your example and how you could append to it:

% GENERATE DATA
n_imgs = 3;
img = double(ones([1024 1024 4]));             % example for image
imgs_dset = double(ones([size(img) n_imgs]));   % dataset [1024 1024 4 n_imgs]
datasetname = '/data';
filename = 'data.h5';

% CREATE hdf5 container
% dset_size is 'Inf' so that container can hold arbitrary number of imgs
dset_size = [size(img) Inf];                   % = [1024 1024 4 Inf]
h5create(filename, datasetname, dset_size, ...
         'Datatype', class(imgs_dset), 'ChunkSize', [1024 1024 1 1] );

% FILL dataset with first few images
h5write(filename, datasetname, imgs_dset, [1 1 1 1], [size(img) n_imgs]);

% APPEND to dataset
n_new_images = 5;
new_imgs_dset = double(ones([size(img) n_new_images]));
info = h5info('data.h5', '/data');
curSize = info.Dataspace.Size; % is currently [1024 1024 4 3].
h5write(filename, datasetname, new_imgs_dset, ...
             [1 1 1 curSize(end)+1], [size(img) n_new_images]);

% check size of dataset after appending:
info = h5info('data.h5', '/data');
disp(info.Dataspace.Size) % size is now [1024 1024 4 8]

Upvotes: 2

Related Questions