|
| 1 | +function h5ex_t_objref |
| 2 | + %%************************************************************************** |
| 3 | + %% This example is adapted from the original file generously provided |
| 4 | + %% by a user to the HDFGroup examples. |
| 5 | + %% |
| 6 | + %% This example shows how to read and write object references |
| 7 | + %% to a dataset. The program first creates objects in the |
| 8 | + %% file and writes references to those objects to a dataset |
| 9 | + %% with a dataspace of DIM0, then closes the file. Next, it |
| 10 | + %% reopens the file, dereferences the references, and outputs |
| 11 | + %% the names of their targets to the screen. |
| 12 | + %% |
| 13 | + %% This file is intended for use with HDF5 Library version 1.8 |
| 14 | + %% EDIT: Also tested with version 1.10. |
| 15 | + %%************************************************************************** |
| 16 | + |
| 17 | + fileName = tempname (); |
| 18 | + DATASET = 'DS1'; |
| 19 | + DIM0 = 2; |
| 20 | + |
| 21 | + dims =DIM0; |
| 22 | + |
| 23 | + %% |
| 24 | + %% Create a new file using the default properties. |
| 25 | + %% |
| 26 | + file = H5F.create (fileName, 'H5F_ACC_TRUNC', 'H5P_DEFAULT', 'H5P_DEFAULT'); |
| 27 | + |
| 28 | + %% |
| 29 | +%%%% Create a dataset with a scalar dataspace. |
| 30 | + %% |
| 31 | + space = H5S.create ('H5S_NULL'); |
| 32 | + obj = H5D.create (file, 'DS2', 'H5T_STD_I32LE', space, 'H5P_DEFAULT'); |
| 33 | + H5D.close (obj); |
| 34 | + H5S.close (space); |
| 35 | + |
| 36 | + %% |
| 37 | + %% Create a group. |
| 38 | + %% |
| 39 | + size_hint=H5ML.get_constant_value('H5P_DEFAULT'); |
| 40 | + obj = H5G.create (file, 'G1', size_hint); |
| 41 | + H5G.close (obj); |
| 42 | + |
| 43 | + %% |
| 44 | + %% Create references to the previously created objects. Passing -1 |
| 45 | + %% as space_id causes this parameter to be ignored. Other values |
| 46 | + %% besides valid dataspaces result in an error. |
| 47 | + %% |
| 48 | + wdata(1) = H5R.create (file, 'G1', 'H5R_OBJECT', -1); |
| 49 | + wdata(2) = H5R.create (file, 'DS2', 'H5R_OBJECT', -1); |
| 50 | + |
| 51 | + %% |
| 52 | + %% Create dataspace. Setting maximum size to [] sets the maximum |
| 53 | + %% size to be the current size. |
| 54 | + %% |
| 55 | + space = H5S.create_simple (1,fliplr(dims), []); |
| 56 | + |
| 57 | + %% |
| 58 | + %% Create the dataset and write the object references to it. |
| 59 | + %% |
| 60 | + dset = H5D.create (file, DATASET, 'H5T_STD_REF_OBJ', space, 'H5P_DEFAULT'); |
| 61 | + H5D.write (dset, 'H5T_STD_REF_OBJ', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT',wdata); |
| 62 | + |
| 63 | + %% |
| 64 | + %% Close and release resources. |
| 65 | + %% |
| 66 | + H5D.close (dset); |
| 67 | + H5S.close (space); |
| 68 | + H5F.close (file); |
| 69 | + |
| 70 | + %% |
| 71 | + %% Now we begin the read section of this example. Here we assume |
| 72 | + %% the dataset has the same name and rank, but can have any size. |
| 73 | + %% |
| 74 | + |
| 75 | + %% |
| 76 | + %% Open file and dataset. |
| 77 | + %% |
| 78 | + file = H5F.open (fileName, 'H5F_ACC_RDONLY', 'H5P_DEFAULT'); |
| 79 | + dset = H5D.open (file, DATASET); |
| 80 | + |
| 81 | + %% |
| 82 | + %% Get dataspace. |
| 83 | + %% |
| 84 | + space = H5D.get_space (dset); |
| 85 | + [~, dims] = H5S.get_simple_extent_dims (space); |
| 86 | + dims = fliplr(dims); |
| 87 | + |
| 88 | + %% |
| 89 | + %% Read the data. |
| 90 | + %% |
| 91 | + rdata = H5D.read (dset, 'H5T_STD_REF_OBJ', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT'); |
| 92 | + |
| 93 | + %% |
| 94 | + %% Output the data to the screen. |
| 95 | + %% |
| 96 | + links = {}; |
| 97 | + for i=1: dims(1) |
| 98 | + obj = H5R.dereference (dset, 'H5R_OBJECT', rdata(i)); |
| 99 | + objtype = H5R.get_obj_type (dset, 'H5R_OBJECT', rdata(i)); |
| 100 | + |
| 101 | + %% |
| 102 | + %% Retrieve the name. |
| 103 | + %% |
| 104 | + name = H5I.get_name(obj); |
| 105 | + |
| 106 | + switch (objtype) |
| 107 | + case H5ML.get_constant_value('H5O_TYPE_GROUP') |
| 108 | + links = [links ["Group " name]]; |
| 109 | + case H5ML.get_constant_value('H5O_TYPE_DATASET') |
| 110 | + links = [links ["Dataset " name]]; |
| 111 | + case H5ML.get_constant_value('H5O_TYPE_TYPE') |
| 112 | + links = [links ["Type " name]]; |
| 113 | + end |
| 114 | + H5O.close (obj); |
| 115 | + end |
| 116 | + |
| 117 | + %% |
| 118 | + %% Close and release resources. |
| 119 | + %% |
| 120 | + H5D.close (dset); |
| 121 | + H5S.close (space); |
| 122 | + H5F.close (file); |
| 123 | + delete (fileName); |
| 124 | + |
| 125 | + ## FIXME: we shouldn't have to transpose here |
| 126 | + assert (rdata', wdata); |
| 127 | + |
| 128 | + assert (links, {"Group /G1", "Dataset /DS2"}); |
| 129 | +endfunction |
0 commit comments