2.1.3. HDF5 in MATLAB¶
- author:
Paul Kienzle, NIST
Note
Editor’s Note: These files were copied directly from an older version of the NeXus documentation (DocBook) and have not been checked that they will run under current Matlab versions.
2.1.3.1. input.dat
¶
This is the same data used with HDF5 in Python.
117.92608 1037
217.92591 1318
317.92575 1704
417.92558 2857
517.92541 4516
617.92525 9998
717.92508 23819
817.92491 31662
917.92475 40458
1017.92458 49087
1117.92441 56514
1217.92425 63499
1317.92408 66802
1417.92391 66863
1517.92375 66599
1617.92358 66206
1717.92341 65747
1817.92325 65250
1917.92308 64129
2017.92291 63044
2117.92275 60796
2217.92258 56795
2317.92241 51550
2417.92225 43710
2517.92208 29315
2617.92191 19782
2717.92175 12992
2817.92158 6622
2917.92141 4198
3017.92125 2248
3117.92108 1321
2.1.3.2. writing data¶
basic_writer.m: Write a NeXus HDF5 file using Matlab
1% Writes a NeXus HDF5 file using matlab
2
3disp 'Write a NeXus HDF5 file'
4filename = 'prj_test.nexus.hdf5';
5timestamp = '2010-10-18T17:17:04-0500';
6
7% read input data
8A = load('input.dat');
9mr = A(:,1);
10I00 = int32(A(:,2));
11
12% clear out old file, if it exists
13
14delete(filename);
15
16% using the simple h5 interface, there is no way to create a group without
17% first creating a dataset; creating the dataset creates all intervening
18% groups.
19
20% store x
21h5create(filename,'/entry/mr_scan/mr',[length(mr)]);
22h5write(filename,'/entry/mr_scan/mr',mr);
23h5writeatt(filename,'/entry/mr_scan/mr','units','degrees');
24h5writeatt(filename,'/entry/mr_scan/mr','long_name','USAXS mr (degrees)');
25
26% store y
27h5create(filename,'/entry/mr_scan/I00',[length(I00)],'DataType','int32');
28h5write(filename,'/entry/mr_scan/I00',I00);
29h5writeatt(filename,'/entry/mr_scan/I00','units','counts');
30h5writeatt(filename,'/entry/mr_scan/I00','long_name','USAXS I00 (counts)');
31
32% indicate that we are plotting y vs. x
33h5writeatt(filename,'/','default','entry');
34h5writeatt(filename,'/entry','default','mr_scan');
35h5writeatt(filename,'/entry/mr_scan','signal','I00');
36h5writeatt(filename,'/entry/mr_scan','axes','mr_scan');
37h5writeatt(filename,'/entry/mr_scan','mr_scan_indices', int32(0));
38
39% add NeXus metadata
40h5writeatt(filename,'/','file_name',filename);
41h5writeatt(filename,'/','file_time',timestamp);
42h5writeatt(filename,'/','instrument','APS USAXS at 32ID-B');
43h5writeatt(filename,'/','creator','basic_writer.m');
44h5writeatt(filename,'/','NeXus_version','4.3.0');
45h5writeatt(filename,'/','HDF5_Version','1.6'); % no 1.8 features used in this example
46h5writeatt(filename,'/entry','NX_class','NXentry');
47h5writeatt(filename,'/entry/mr_scan','NX_class','NXdata');
48
49
50h5disp(filename);
2.1.3.3. reading data¶
basic_reader.m: Read a NeXus HDF5 file using Matlab
1% Reads NeXus HDF5 file and print the contents
2
3filename = 'prj_test.nexus.hdf5';
4root = h5info(filename,'/');
5attrs = root.Attributes;
6for i = 1:length(attrs)
7 fprintf('%s: %s\n', attrs(i).Name, attrs(i).Value);
8end
9mr = h5read(filename,'/entry/mr_scan/mr');
10i00 = h5read(filename, '/entry/mr_scan/I00');
11fprintf('#\t%s\t%s\n','mr','I00');
12for i = 1:length(mr)
13 fprintf('%d\t%g\t%d\n', i, mr(i), i00(i));
14end
2.1.3.4. writing data file with links¶
writer_2_1.m: Write a NeXus HDF5 file with links
1% Writes a simple NeXus HDF5 file with links
2% according to the example from Figure 2.1 in the Design chapter
3
4filename = 'writer_2_1.hdf5';
5
6% read input data
7A = load('input.dat');
8two_theta = A(:,1);
9counts = int32(A(:,2));
10
11% clear out old file, if it exists
12delete(filename);
13
14% store x
15h5create(filename,'/entry/instrument/detector/two_theta',[length(two_theta)]);
16h5write(filename,'/entry/instrument/detector/two_theta',two_theta);
17h5writeatt(filename,'/entry/instrument/detector/two_theta','units','degrees');
18
19% store y
20h5create(filename,'/entry/instrument/detector/counts',[length(counts)],'DataType','int32');
21h5write(filename,'/entry/instrument/detector/counts',counts);
22h5writeatt(filename,'/entry/instrument/detector/counts','units','counts');
23
24% create group NXdata with links to detector
25% note: requires the additional file h5link.m
26h5link(filename,'/entry/instrument/detector/two_theta','/entry/data/two_theta');
27h5link(filename,'/entry/instrument/detector/counts','/entry/data/counts');
28
29% indicate that we are plotting y vs. x
30h5writeatt(filename,'/','default','entry');
31h5writeatt(filename,'/entry','default','data');
32h5writeatt(filename,'/entry/data','signal','counts');
33h5writeatt(filename,'/entry/data','axes','two_theta');
34h5writeatt(filename,'/entry/data','two_theta_indices',int32(0));
35
36% add NeXus metadata
37h5writeatt(filename,'/','file_name',filename);
38h5writeatt(filename,'/','file_time',timestamp);
39h5writeatt(filename,'/','instrument','APS USAXS at 32ID-B');
40h5writeatt(filename,'/','creator','writer_2_1.m');
41h5writeatt(filename,'/','NeXus_version','4.3.0');
42h5writeatt(filename,'/','HDF5_Version','1.6'); % no 1.8 features used in this example
43h5writeatt(filename,'/entry','NX_class','NXentry');
44h5writeatt(filename,'/entry/instrument','NX_class','NXinstrument');
45h5writeatt(filename,'/entry/instrument/detector','NX_class','NXdetector');
46h5writeatt(filename,'/entry/data','NX_class','NXdata');
47
48% show structure of the file that was created
49h5disp(filename);
h5link.m: support module for creating NeXus-style HDF5 hard links
1function h5link(filename, from, to)
2%H5LINK Create link to an HDF5 dataset.
3% H5LINK(FILENAME,SOURCE,TARGET) creates an HDF5 link from the
4% dataset at location SOURCE to a dataset at location TARGET. All
5% intermediate groups in the path to target are created.
6%
7% Example: create a link from /hello/world to /goodbye/world
8% h5create('myfile.h5','/hello/world',[100 200]);
9% h5link('myfile.h5','/hello/world','/goodbye/world');
10% hgdisp('myfile.h5');
11%
12% See also: h5create, h5read, h5write, h5info, h5disp
13
14% split from and to into group/dataset
15idx = strfind(from,'/');
16from_path = from(1:idx(end)-1);
17from_data = from(idx(end)+1:end);
18idx = strfind(to,'/');
19to_path = to(1:idx(end)-1);
20to_data = to(idx(end)+1:end);
21
22% open the HDF file
23fid = H5F.open(filename,'H5F_ACC_RDWR','H5P_DEFAULT');
24
25% create target group if it doesn't already exist
26create_intermediate = H5P.create('H5P_LINK_CREATE');
27H5P.set_create_intermediate_group(create_intermediate, 1);
28try
29 H5G.create(fid,to_path,create_intermediate,'H5P_DEFAULT','H5P_DEFAULT');
30catch
31end
32H5P.close(create_intermediate);
33
34% open groups and create link
35from_id = H5G.open(fid, from_path);
36to_id = H5G.open(fid, to_path);
37H5L.create_hard(from_id, from_data, to_id, to_data, 'H5P_DEFAULT','H5P_DEFAULT');
38
39% close all
40H5G.close(from_id);
41H5G.close(to_id);
42H5F.close(fid);
43end
2.1.3.5. Downloads¶
file |
description |
---|---|
two-column text data file, also used in other examples |
|
writes a NeXus HDF5 file using |
|
reads the NeXus HDF5 file written by |
|
support module for creating NeXus-style HDF5 hard links |
|
like |