MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

7 Working with Scientific Data Formats Note SDgetinfo returns dimension values in row-major order, the ordering used by HDF4. Because MATLAB stores data in column-major order, you must specify the dimensions in column-major order, that is, [columns,rows]. In addition, you must use zero-based indexing in these arguments. For example, to read the entire contents of a data set, use this code: [ds_name, ds_ndims, ds_dims, ds_type, ds_atts, stat] = hdfsd('getinfo',sds_id); ds_start = zeros(1,ds_ndims); % Creates the vector [0 0] ds_stride = []; ds_edges = ds_dims; [ds_data, status] = hdfsd('readdata',sds_id,ds_start,ds_stride,ds_edges); disp(ds_data) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 To read less than the entire data set, use the start, stride, and edges vectors to specify where you want to start reading data and how much data you want to read. For example, this code reads the entire second row of the sample data set: ds_start = [0 1]; % Start reading at the first column, second row ds_stride = []; % Read each element ds_edges = [5 1]; % Read a 1-by-5 vector of data [ds_data, status] = hdfsd('readdata',sds_id,ds_start,ds_stride,ds_edges); Step 7: Closing the HDF4 Data Set. After writing data to a data set in an HDF4 file, you must close access to the data set. In the HDF4 SD API, you use the SDendaccess routine to close a data set. In MATLAB, use the hdfsd function, specifying as arguments: 7-62

Hierarchical Data Format (HDF4) Files • Name of the SD API routine, endaccess in this case • HDF4 SD data set identifier (sds_id) returnedbySDselect Forexample,thiscodeclosesthedataset: stat = hdfsd('endaccess',sds_id); You must close access to all the data sets in an HDF4 file before closing it. Step 8: Closing the HDF4 File. After writing data to a data set and closing the data set, you must also close the HDF4 file. In the HDF4 SD API, you use the SDend routine. In MATLAB, use the hdfsd function, specifying as arguments: • Name of the SD API routine, end in this case • HDF4 SD file identifier (sd_id) returnedbySDstart Forexample,thiscodeclosesthedataset: stat = hdfsd('end',sd_id); Example: Exporting Data Using the HDF4 SD API Functions The following sections provide a step-by-step example of how to export data from the MATLAB workspace to an HDF4 file using Scientific Data (SD) API functions. • “Step 1: Creating an HDF4 File” on page 7-64 • “Step 2: Creating an HDF4 Data Set” on page 7-64 • “Step 3: Writing MATLAB Data to an HDF4 File” on page 7-66 • “Step 4: Writing Metadata to an HDF4 File” on page 7-68 • “Step 5: Closing HDF4 Data Sets” on page 7-69 • “Step 6: Closing an HDF4 File” on page 7-70 7-63

Hierarchical Data Format (HDF4) Files<br />

• Name of the SD API routine, endaccess in this case<br />

• HDF4 SD data set identifier (sds_id) returnedbySDselect<br />

Forexample,thiscodeclosesthedataset:<br />

stat = hdfsd('endaccess',sds_id);<br />

You must close access to all the data sets in an HDF4 file before closing it.<br />

Step 8: Closing the HDF4 File. After writing data to a data set and closing<br />

the data set, you must also close the HDF4 file. In the HDF4 SD API, you<br />

use the SDend routine. In <strong>MATLAB</strong>, use the hdfsd function, specifying as<br />

arguments:<br />

• Name of the SD API routine, end in this case<br />

• HDF4 SD file identifier (sd_id) returnedbySDstart<br />

Forexample,thiscodeclosesthedataset:<br />

stat = hdfsd('end',sd_id);<br />

Example: Exporting Data Using the HDF4 SD API Functions<br />

The following sections provide a step-by-step example of how to export data<br />

from the <strong>MATLAB</strong> workspace to an HDF4 file using Scientific Data (SD)<br />

API functions.<br />

• “Step 1: Creating an HDF4 File” on page 7-64<br />

• “Step 2: Creating an HDF4 Data Set” on page 7-64<br />

• “Step 3: Writing <strong>MATLAB</strong> Data to an HDF4 File” on page 7-66<br />

• “Step 4: Writing Metadata to an HDF4 File” on page 7-68<br />

• “Step 5: Closing HDF4 Data Sets” on page 7-69<br />

• “Step 6: Closing an HDF4 File” on page 7-70<br />

7-63

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!