2.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.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.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.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.2.1.3.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