function status = h5_ref2obj %H5_REF2OBJ This example shows how to create and store references to objects. % Program creates a file, two groups, a dataset to store integer data and % a dataset to store references to the objects. Stored references are % used to open the objects they are point to. Data is written to the % dereferenced dataset, and class type is displayed for the shared % datatype. % % status is returned to indicate whether the example ran successfully % Copyright 2006 The MathWorks, Inc. % $Revision: 1.1.4.1 $ $Date: 2006/05/10 19:51:08 $ % Initialize status to false status = false; fname = qeTempFile('h5'); filename = fname.fullpath; % file name dsetnamei = 'INTEGERS'; % dataset with integer data dsetnamer = 'OBJECT_REFERENCES'; % dataset with object references groupname1 = 'GROUP1'; % groups in the file groupname2 = 'GROUP2'; dims1 = 5; dimsr = 4; my_maxdims = 5; rank = 1; rankr = 1; data = int32(1:5); % Create file with default file access and file creation properties. fileid = H5F.create(filename, 'H5F_ACC_TRUNC', 'H5P_DEFAULT', 'H5P_DEFAULT'); % Create a group in the file. grp1_id = H5G.create(fileid, groupname1, -1); % Create a group inside the created gorup. grp2_id = H5G.create(grp1_id, groupname2, -1); % Create dataspaces for datasets. space_id = H5S.create_simple(rank, dims1, my_maxdims); spacer_id = H5S.create_simple(rankr, dimsr, []); % Create integer dataset. dset_id = H5D.create(fileid, dsetnamei, 'H5T_NATIVE_INT', space_id, 'H5P_DEFAULT'); % Create dataset to store references to the objects. dsetr_id = H5D.create(fileid, dsetnamer, 'H5T_STD_REF_OBJ', spacer_id, 'H5P_DEFAULT'); % Create a datatype and store in the file. type_id = H5T.copy('H5T_NATIVE_FLOAT'); H5T.commit(fileid, 'MYTYPE', type_id); % Close dataspaces, groups and integer dataset. H5S.close(space_id); H5S.close(spacer_id); H5T.close(type_id); H5D.close(dset_id); H5G.close(grp1_id); H5G.close(grp2_id); % Create references to two groups, integer dataset and shared datatype % and write it to the dataset in the file. ref(:,1) = H5R.create(fileid, groupname1, 'H5R_OBJECT', -1); ref(:,2) = H5R.create(fileid, '/GROUP1/GROUP2', 'H5R_OBJECT', -1); ref(:,3) = H5R.create(fileid, dsetnamei, 'H5R_OBJECT', -1); ref(:,4) = H5R.create(fileid, 'MYTYPE', 'H5R_OBJECT', -1); H5D.write(dsetr_id, 'H5T_STD_REF_OBJ', 'H5S_ALL', 'H5S_ALL',... 'H5P_DEFAULT', ref); % Close the dataset. H5D.close(dsetr_id); % Reopen the dataset with object references and read references to the buffer. dsetr_id = H5D.open( fileid, dsetnamer); ref_out = H5D.read(dsetr_id, 'H5T_STD_REF_OBJ', 'H5S_ALL', 'H5S_ALL',... 'H5P_DEFAULT'); % Dereference the third reference. We know that it is a dataset. On practice % one should use H5Rget_object_type function to find out % the type of an object the reference points to. dset_id = H5R.dereference(dsetr_id, 'H5R_OBJECT', ref(:,3)); % Write data to the dataset. H5D.write(dset_id, 'H5T_NATIVE_INT', 'H5S_ALL' , 'H5S_ALL', 'H5P_DEFAULT', data); % Dereference the fourth reference. We know that it is a datatype. On practice % one should use H5Rget_object_type function to find out % the type of an object the reference points to. type_id = H5R.dereference(dsetr_id, 'H5R_OBJECT', ref(:,4)); % Get datatype class and indicate success if it is of a FLOAT class. class = H5T.get_class(type_id); if H5ML.compare_values(class, 'H5T_FLOAT') status = true; end H5D.close(dset_id); H5D.close(dsetr_id); H5T.close(type_id); H5F.close(fileid);