%% Setup. cd /Users/jeff/Documents/MATLAB %% Example -- Read some data filename = fullfile('hdf5','dHDF5_test.h5'); file = H5F.open(filename, 'H5F_ACC_RDONLY', 'H5P_DEFAULT'); dataset = H5D.open(file, '/images/plane interlace'); data = H5D.read(dataset, 'H5ML_DEFAULT', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT'); imshow(permute(data, [2 1 3])) % Close the dataset and the file. H5D.close(dataset); H5F.close(file); %% Example -- The example from Tuesday. dset_data = reshape(int32(1:24), [6 4]); file_id = H5F.create('dset.h5', 'H5F_ACC_TRUNC', 'H5P_DEFAULT', 'H5P_DEFAULT'); dims = [4 6]; dataspace_id = H5S.create_simple(2, dims, []); dataset_id = H5D.create(file_id, '/dset', 'H5T_STD_I32BE', dataspace_id, ... 'H5P_DEFAULT'); H5D.write(dataset_id, 'H5T_NATIVE_INT', 'H5S_ALL', 'H5S_ALL', ... 'H5P_DEFAULT', dset_data); H5S.close(dataspace_id); H5D.close(dataset_id); H5F.close(file_id); %% Example -- Hyperslab selection. % Open a file and get handles to the dataset and dataspace. file = H5F.open('london_2.h5', 'H5F_ACC_RDONLY', 'H5P_DEFAULT'); dataset = H5D.open(file, '/image'); dataspace = H5D.get_space(dataset); % How big is the data in the file? [ndims, dims] = H5S.get_simple_extent_dims(dataspace); % Let's subset... offset = [0 0 0]; counts = dims; dimStride = []; %offset = [0 1500 1000]; %counts = [3 500 500]; %dimStride = []; %offset = [0 0 0]; %stride = 4; %counts = floor([3, (dims(2) - offset(2))/stride, (dims(3) - offset(3))/stride]); %dimStride = [1 stride stride]; % Read the data. H5S.select_hyperslab(dataspace, 'H5S_SELECT_SET', offset, dimStride, counts, []); memspace = H5S.create_simple(3, counts, []); X_subset = H5D.read(dataset, 'H5T_NATIVE_UINT8', memspace, dataspace, 'H5P_DEFAULT'); % Show the data. imtool(X_subset) % Clean up. H5S.close(dataspace); H5S.close(memspace); H5D.close(dataset); H5F.close(file); %% Example -- Handle Errors. One way... filename = fullfile('hdf5', 'dHDF5_test.h5'); file = H5F.open(filename, 'H5F_ACC_RDONLY', 'H5P_DEFAULT'); try H5D.open(file, '/images/GuessWhat_TheImageIsNotHere'); catch disp('Oops! Something went wrong.') end lasterror %% Another way to handle errors... % Since there is no dogpile dataset, we will walk the error stack H5E.walk('H5E_WALK_UPWARD', @errorIterator) % Close the dataset and the file. H5F.close(file); % Clear the error stack H5E.clear % Since the stack has been cleared, we will not enter the error iterator H5E.walk('H5E_WALK_UPWARD', @errorIterator) %% Example -- Query attributes. file = H5F.open(filename, 'H5F_ACC_RDONLY', 'H5P_DEFAULT'); dataset = H5D.open(file, '/images/plane interlace'); numAtts = H5A.get_num_attrs(dataset) % Open attribute 0 and get name attr0 = H5A.open_idx(dataset, 0); attrNames = H5A.get_name(attr0) % Open attribute 1 and get type attr1 = H5A.open_idx(dataset, 1); attrType = H5A.get_type(attr1) attrClass = H5T.get_class(attrType) actTypeComparison = H5ML.compare_values(attrClass, 'H5T_STRING') % Open attribute 2 and get space attr2 = H5A.open_idx(dataset, 2); attrSpace = H5A.get_space(attr2); numPoints = H5S.get_simple_extent_npoints(attrSpace) %% Look at the contents of an object get(file) get(dataset) get(attrSpace) % Notice that the objects contain a callback to close the object at % destruction and a listener to prevent the objects from doing bad things % when the HDF5 identifiers become stale. %% Example -- Get the attribute data. file = H5F.open(filename, 'H5F_ACC_RDONLY', 'H5P_DEFAULT'); dataset = H5D.open(file, '/images/plane interlace'); % Open attribute IMAGE_MINMAXRANGE attr = H5A.open_name(dataset, 'IMAGE_MINMAXRANGE'); % Read from attribute. Note the use of H5ML_DEFAULT minMaxRange = H5A.read(attr, 'H5ML_DEFAULT') % Clean up. H5A.close(attr); H5D.close(dataset); H5F.close(file); %% Example -- Add an attribute to a dataset. filename = 'MyNewFile.h5'; file = H5F.create(filename, 'H5F_ACC_TRUNC', ... 'H5P_DEFAULT', 'H5P_DEFAULT'); % Create a group and dataset group = H5G.create(file, '/Data', 0); rank = 1; dims = 7; dataspace = H5S.create_simple(rank, dims, []); dataset = H5D.create(file, '/Data/Fibonacci', 'H5T_STD_I32LE', ... dataspace, 'H5P_DEFAULT'); H5D.write(dataset, 'H5ML_DEFAULT', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', ... int32([1 1 2 3 5 8 13])); % Create attribute rank = 2; dims = [2, 2]; attrDataspace = H5S.create_simple(rank, dims, []); attrName = 'CDATA_RANGE'; attr = H5A.create(dataset, attrName, 'H5T_STD_I32LE', ... attrDataspace, 'H5P_DEFAULT'); % Write attribute attrData = int32([1, 2; 3, 4]); H5A.write(attr, 'H5ML_DEFAULT', attrData); % Close attribute H5A.close(attr); %% % Verify the attribute is present H5A.get_num_attrs(dataset) % Read attribute data attr2 = H5A.open_idx(dataset, 0); H5A.get_name(attr2) H5A.read(attr2, 'H5ML_DEFAULT') % Delete attribute H5A.delete(dataset, attrName); % Verify deletion H5A.get_num_attrs(dataset) % Close open identifiers H5A.close(attr2); H5S.close(attrDataspace); H5D.close(dataset); H5S.close(dataspace); H5G.close(group); H5F.close(file); %% Look at the file with h5dump !hdf5/h5dump MyNewFile.h5 %% Example - Create a variable-length array. % Create vlen datatype datatype = H5T.vlen_create('H5T_STD_I8LE'); % Create dataspace rank = 2; dims = [4 1]; dataspace = H5S.create_simple(rank, dims, []); % Create file and dataset filename = 'MyVlenFile.h5'; file = H5F.create(filename, 'H5F_ACC_TRUNC', 'H5P_DEFAULT',... 'H5P_DEFAULT'); dataset = H5D.create(file, 'dataset1', datatype, dataspace, ... 'H5P_DEFAULT'); % Create the VL data for i = 1:prod(dims) vlData{i} = int8(magic(i+3)); end % Write the data H5D.write(dataset, datatype, 'H5S_ALL', 'H5S_ALL',... 'H5P_DEFAULT', vlData); % Get size H5D.vlen_get_buf_size(dataset, datatype, dataspace); % Expected size: magic(1) - 1 byte, magic(2)- 4 bytes, magic(3) - 9 bytes, % magic(4) - 16 bytes - Total 30 bytes % Close open identifiers H5T.close(datatype); H5S.close(dataspace); H5D.close(dataset); H5F.close(file); %% Look at the file with h5dump !hdf5/h5dump MyVlenFile.h5 %% Example -- Use an iterator. filename = fullfile('hdf5', 'dHDF5_test.h5'); file = H5F.open(filename, 'H5F_ACC_RDONLY', 'H5P_DEFAULT'); dataset = H5D.open(file, '/images/plane interlace'); % Call the same function on each attribute in the dataset. % (See attributeIterator.m) H5A.iterate(dataset, 0, @attributeIterator); % Close the dataset and the file. H5D.close(dataset); H5F.close(file); %% More examples -- References addpath /Users/jeff/Documents/MATLAB/hdf5/ %h5_ref2reg