function status = h5_ref2reg %H5_REF2REG This example shows how to create, store and dereference %references to the dataset regions. % % It creates a file and writes a two dimensional integer dataset to it. % Then it creates a dataset to store region references in. It stores % references to a hyperslab and 3 points selected (for the integer % dataset previously created). % % It then reopens the references dataset, reads and dereferences the % region references, and then reads and displays the selected hyperslab % and selected elements data from the integer dataset. % % status is returned to indicate whether the example ran successfully % Copyright 2006 The MathWorks, Inc. % $Revision: 1.1.4.1 $ $Date: 2006/05/23 23:21:44 $ % Initialize status to false status = false; fname = qeTempFile('h5'); filename = fname.fullpath; dsetnamev = 'MATRIX'; dsetnamer = 'REGION_REFERENCES'; dims = [2,9]; dimsr = [2]; rank = 2; rankr = 1; data = int32([1,1,2,3,3,4,5,5,6, 1,2,2,3,4,4,5,6,6]); coord = [0, 0, 1 6, 0, 8]; % Create file with default file access and file creation properties. file_id = H5F.create(filename, 'H5F_ACC_TRUNC', 'H5P_DEFAULT', 'H5P_DEFAULT'); % Create dataspace for datasets. space_id = H5S.create_simple(rank, dims, []); spacer_id = H5S.create_simple(rankr, dimsr, []); % Create integer dataset. dsetv_id = H5D.create(file_id, dsetnamev, 'H5T_NATIVE_INT', space_id, ... 'H5P_DEFAULT'); % Write data to the dataset. H5D.write(dsetv_id, 'H5T_NATIVE_INT', 'H5S_ALL' , 'H5S_ALL', 'H5P_DEFAULT', ... data); H5D.close(dsetv_id); % Dataset with references. dsetr_id = H5D.create(file_id, dsetnamer, 'H5T_STD_REF_DSETREG', spacer_id, ... 'H5P_DEFAULT'); % Create a reference to the hyperslab. start = [0 3]; count = [2 3]; H5S.select_hyperslab(space_id,'H5S_SELECT_SET',start,[],count,[]); ref1 = H5R.create(file_id, dsetnamev, 'H5R_DATASET_REGION', space_id); % Create a reference to elements selection. H5S.select_none(space_id); H5S.select_elements(space_id, 'H5S_SELECT_SET', coord); ref2 = H5R.create(file_id, dsetnamev, 'H5R_DATASET_REGION', space_id); % Write dataset with the references. H5D.write(dsetr_id, 'H5T_STD_REF_DSETREG', 'H5S_ALL', 'H5S_ALL', ... 'H5P_DEFAULT', [ref1 ref2]); % Close all objects. H5S.close(space_id); H5S.close(spacer_id); H5D.close(dsetr_id); H5F.close(file_id); % Reopen the file to read selections back. file_id = H5F.open(filename, 'H5F_ACC_RDWR', 'H5P_DEFAULT'); % Reopen the dataset with object references and read references to the buffer. dsetr_id = H5D.open( file_id, dsetnamer); ref_out = H5D.read(dsetr_id, 'H5T_STD_REF_DSETREG', 'H5S_ALL', 'H5S_ALL', ... 'H5P_DEFAULT'); % Dereference the first reference. dsetv_id = H5R.dereference(dsetr_id, 'H5R_DATASET_REGION', ref_out(:,1)); space_id = H5R.get_region(dsetr_id, 'H5R_DATASET_REGION', ref_out(:,1)); % Read and display hyperslab selection from the dataset. data_out = H5D.read(dsetv_id, 'H5T_NATIVE_INT', 'H5S_ALL', space_id, ... 'H5P_DEFAULT'); % Close dataspace and the dataset. H5S.close(space_id); H5D.close(dsetv_id); % Initialize data_out array again to get point selection. data_out = zeros(8,1,'uint32'); % Dereference the second reference. dsetv_id = H5R.dereference(dsetr_id, 'H5R_DATASET_REGION', ref_out(:,2)); space_id = H5R.get_region(dsetv_id, 'H5R_DATASET_REGION',ref_out(:,2)); % Read selected data from the dataset. data_out = H5D.read(dsetv_id, 'H5T_NATIVE_INT', 'H5S_ALL', space_id,... 'H5P_DEFAULT'); % Close dataspace and the dataset. H5S.close(space_id); H5D.close(dsetv_id); H5D.close(dsetr_id); H5F.close(file_id); % Example is successful if we did not error out in the statements above status = true;