function h5link(filename, from, to) %H5LINK Create link to an HDF5 dataset. % H5LINK(FILENAME,SOURCE,TARGET) creates an HDF5 link from the % dataset at location SOURCE to a dataset at location TARGET. All % intermediate groups in the path to target are created. % % Example: create a link from /hello/world to /goodbye/world % h5create('myfile.h5','/hello/world',[100 200]); % h5link('myfile.h5','/hello/world','/goodbye/world'); % hgdisp('myfile.h5'); % % See also: h5create, h5read, h5write, h5info, h5disp % split from and to into group/dataset idx = strfind(from,'/'); from_path = from(1:idx(end)-1); from_data = from(idx(end)+1:end); idx = strfind(to,'/'); to_path = to(1:idx(end)-1); to_data = to(idx(end)+1:end); % open the HDF file fid = H5F.open(filename,'H5F_ACC_RDWR','H5P_DEFAULT'); % create target group if it doesn't already exist create_intermediate = H5P.create('H5P_LINK_CREATE'); H5P.set_create_intermediate_group(create_intermediate, 1); try H5G.create(fid,to_path,create_intermediate,'H5P_DEFAULT','H5P_DEFAULT'); catch end H5P.close(create_intermediate); % open groups and create link from_id = H5G.open(fid, from_path); to_id = H5G.open(fid, to_path); H5L.create_hard(from_id, from_data, to_id, to_data, 'H5P_DEFAULT','H5P_DEFAULT'); % close all H5G.close(from_id); H5G.close(to_id); H5F.close(fid); end