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 Step 3: Writing MATLAB Data to an HDF4 File. After creating an HDF4 file and creating a data set in the file, you can write data to the entire data set or just a portion of the data set. In the HDF4 SD API, you use the SDwritedata routine. In MATLAB, use the hdfsd function, specifying as arguments: • Name of the SD API routine, 'writedata' in this case • ValidHDF4SDdatasetidentifier,sds_id, returned by SDcreate • Location in the data set where you want to start writing data, called the start vector in HDF4 terminology • Number of elements along each dimension to skip between each write operation, called the stride vector in HDF4 terminology • Total number of elements to write along each dimension, called the edges vector in HDF4 terminology • MATLAB array to be written Note You must specify the values of the start, stride, and edges arguments in row-major order, rather than the column-major order used in MATLAB. Note how the example uses fliplr to reverse the order of the dimensions in the vector returned by the size function before assigning it as the value of the edges argument. The values you assign to these arguments depend on the MATLAB array you want to export. For example, the following code fragment writes this MATLAB 3-by-5 array of doubles, A = [ 1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15 ]; into an HDF4 file: ds_start = zeros(1:ndims(A)); % Start at the beginning ds_stride = []; % Write every element. ds_edges = fliplr(size(A)); % Reverse the dimensions. stat = hdfsd('writedata',sds_id,... ds_start, ds_stride, ds_edges, A); 7-66

Hierarchical Data Format (HDF4) Files If it can write the data to the data set, SDwritedata returns 0; otherwise, it returns -1. Note SDwritedata queues write operations. To ensure that these queued write operations are executed, you must close the file, using the SDend routine. See “Step 6: Closing an HDF4 File” on page 7-70 for more information. As a convenience, MATLAB provides a function, MLcloseall, thatyoucanuseto close all open data sets and file identifiers with a single call. See “Using the MATLAB HDF4 Utility API” on page 7-70 for more information. To write less than the entire data set, use the start, stride, and edges vectors to specify where you want to start writing data and how much data you want to write. For example, the following code fragment uses SDwritedata to replace the values of the entire second row of the sample data set: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 with the vector B: B = [ 9 9 9 9 9]; In the example, the start vector specifies that you want to start the write operationinthefirstcolumnofthe second row. Note how HDF4 uses zero-based indexing and specifies the column dimension first. In MATLAB, you would specify this location as (2,1). The edges argument specifies the dimensions of the data to be written. Note that the size of the array of data to be written must match the edgespecification. ds_start = [0 1]; % Start writing at the first column, second row. ds_stride = []; % Write every element. ds_edges = [5 1]; % Each row is a 1-by-5 vector. stat = hdfsd('writedata',sds_id,ds_start,ds_stride,ds_edges,B); 7-67

7 Working with Scientific Data Formats<br />

Step 3: Writing <strong>MATLAB</strong> Data to an HDF4 File. After creating an<br />

HDF4 file and creating a data set in the file, you can write data to the entire<br />

data set or just a portion of the data set. In the HDF4 SD API, you use the<br />

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

arguments:<br />

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

• ValidHDF4SDdatasetidentifier,sds_id, returned by SDcreate<br />

• Location in the data set where you want to start writing data, called the<br />

start vector in HDF4 terminology<br />

• Number of elements along each dimension to skip between each write<br />

operation, called the stride vector in HDF4 terminology<br />

• Total number of elements to write along each dimension, called the edges<br />

vector in HDF4 terminology<br />

• <strong>MATLAB</strong> array to be written<br />

Note You must specify the values of the start, stride, and edges arguments<br />

in row-major order, rather than the column-major order used in <strong>MATLAB</strong>.<br />

Note how the example uses fliplr to reverse the order of the dimensions in<br />

the vector returned by the size function before assigning it as the value of<br />

the edges argument.<br />

The values you assign to these arguments depend on the <strong>MATLAB</strong> array<br />

you want to export. For example, the following code fragment writes this<br />

<strong>MATLAB</strong> 3-by-5 array of doubles,<br />

A = [ 1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15 ];<br />

into an HDF4 file:<br />

ds_start = zeros(1:ndims(A)); % Start at the beginning<br />

ds_stride = [];<br />

% Write every element.<br />

ds_edges = fliplr(size(A)); % Reverse the dimensions.<br />

stat = hdfsd('writedata',sds_id,...<br />

ds_start, ds_stride, ds_edges, A);<br />

7-66

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

Saved successfully!

Ooh no, something went wrong!