2.1.4. 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.4.1. input.dat

This is the same data used with HDF5 in Python with h5py.

 1 17.92608    1037
 2 17.92591    1318
 3 17.92575    1704
 4 17.92558    2857
 5 17.92541    4516
 6 17.92525    9998
 7 17.92508    23819
 8 17.92491    31662
 9 17.92475    40458
10 17.92458    49087
11 17.92441    56514
12 17.92425    63499
13 17.92408    66802
14 17.92391    66863
15 17.92375    66599
16 17.92358    66206
17 17.92341    65747
18 17.92325    65250
19 17.92308    64129
20 17.92291    63044
21 17.92275    60796
22 17.92258    56795
23 17.92241    51550
24 17.92225    43710
25 17.92208    29315
26 17.92191    19782
27 17.92175    12992
28 17.92158    6622
29 17.92141    4198
30 17.92125    2248
31 17.92108    1321

2.1.4.2. writing data

basic_writer.m: Write a NeXus HDF5 file using Matlab

 1 % Writes a NeXus HDF5 file using matlab
 2 
 3 disp 'Write a NeXus HDF5 file'
 4 filename = 'prj_test.nexus.hdf5';
 5 timestamp = '2010-10-18T17:17:04-0500';
 6 
 7 % read input data
 8 A = load('input.dat');
 9 mr = A(:,1);
10 I00 = int32(A(:,2));
11 
12 % clear out old file, if it exists
13 
14 delete(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
21 h5create(filename,'/entry/mr_scan/mr',[length(mr)]);
22 h5write(filename,'/entry/mr_scan/mr',mr);
23 h5writeatt(filename,'/entry/mr_scan/mr','units','degrees');
24 h5writeatt(filename,'/entry/mr_scan/mr','long_name','USAXS mr (degrees)');
25 
26 % store y
27 h5create(filename,'/entry/mr_scan/I00',[length(I00)],'DataType','int32');
28 h5write(filename,'/entry/mr_scan/I00',I00);
29 h5writeatt(filename,'/entry/mr_scan/I00','units','counts');
30 h5writeatt(filename,'/entry/mr_scan/I00','long_name','USAXS I00 (counts)');
31 
32 % indicate that we are plotting y vs. x
33 h5writeatt(filename,'/','default','entry');
34 h5writeatt(filename,'/entry','default','mr_scan');
35 h5writeatt(filename,'/entry/mr_scan','signal','I00');
36 h5writeatt(filename,'/entry/mr_scan','axes','mr_scan');
37 h5writeatt(filename,'/entry/mr_scan','mr_scan_indices', int32(0));
38 
39 % add NeXus metadata
40 h5writeatt(filename,'/','file_name',filename);
41 h5writeatt(filename,'/','file_time',timestamp);
42 h5writeatt(filename,'/','instrument','APS USAXS at 32ID-B');
43 h5writeatt(filename,'/','creator','basic_writer.m');
44 h5writeatt(filename,'/','NeXus_version','4.3.0');
45 h5writeatt(filename,'/','HDF5_Version','1.6'); % no 1.8 features used in this example
46 h5writeatt(filename,'/entry','NX_class','NXentry');
47 h5writeatt(filename,'/entry/mr_scan','NX_class','NXdata');
48 
49 
50 h5disp(filename);

2.1.4.3. reading data

basic_reader.m: Read a NeXus HDF5 file using Matlab

 1 % Reads NeXus HDF5 file and print the contents
 2 
 3 filename = 'prj_test.nexus.hdf5';
 4 root = h5info(filename,'/');
 5 attrs = root.Attributes;
 6 for i = 1:length(attrs)
 7     fprintf('%s: %s\n', attrs(i).Name, attrs(i).Value);
 8 end
 9 mr = h5read(filename,'/entry/mr_scan/mr');
10 i00 = h5read(filename, '/entry/mr_scan/I00');
11 fprintf('#\t%s\t%s\n','mr','I00');
12 for i = 1:length(mr)
13     fprintf('%d\t%g\t%d\n', i, mr(i), i00(i));
14 end

2.1.4.5. Downloads

file

description

input.dat

two-column text data file, also used in other examples

basic_writer.m

writes a NeXus HDF5 file using input.dat

basic_reader.m

reads the NeXus HDF5 file written by basic_writer.m

h5link.m

support module for creating NeXus-style HDF5 hard links

writer_2_1.m

like basic_writer.m but stores data in /entry/instrument/detector and then links to NXdata group