MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 578 and 579: 7 Working with Scientific Data Form
- Page 580 and 581: 7 Working with Scientific Data Form
- Page 582 and 583: 7 Working with Scientific Data Form
- Page 584 and 585: 7 Working with Scientific Data Form
- Page 586 and 587: 7 Working with Scientific Data Form
- Page 588 and 589: 7 Working with Scientific Data Form
- Page 590 and 591: 7 Working with Scientific Data Form
- Page 592 and 593: 7 Working with Scientific Data Form
- Page 594 and 595: 7 Working with Scientific Data Form
- Page 596 and 597: 7 Working with Scientific Data Form
- Page 598 and 599: 7 Working with Scientific Data Form
- Page 600 and 601: 7 Working with Scientific Data Form
- Page 602 and 603: 7 Working with Scientific Data Form
- Page 604 and 605: 7 Working with Scientific Data Form
- Page 606 and 607: 7 Working with Scientific Data Form
- Page 608 and 609: 7 Working with Scientific Data Form
- Page 610 and 611: 7 Working with Scientific Data Form
- Page 612 and 613: 7 Working with Scientific Data Form
- Page 614 and 615: 7 Working with Scientific Data Form
- Page 616 and 617: 7 Working with Scientific Data Form
- Page 618 and 619: 7 Working with Scientific Data Form
- Page 620 and 621: 7 Working with Scientific Data Form
- Page 622 and 623: 7 Working with Scientific Data Form
- Page 624 and 625: 7 Working with Scientific Data Form
- Page 626 and 627: 7 Working with Scientific Data Form
- Page 630 and 631: 7 Working with Scientific Data Form
- Page 632 and 633: 7 Working with Scientific Data Form
- Page 634 and 635: 7 Working with Scientific Data Form
- Page 636 and 637: 8 Error Handling Checking for Error
- Page 638 and 639: 8 Error Handling Handling and Recov
- Page 640 and 641: 8 Error Handling X = A * B catch di
- Page 642 and 643: 8 Error Handling matrixMultiply(A,
- Page 644 and 645: 8 Error Handling Message Identifier
- Page 646 and 647: 8 Error Handling error('msg_id', 'e
- Page 648 and 649: 8 Error Handling Warnings Like erro
- Page 650 and 651: 8 Error Handling Warning Control Th
- Page 652 and 653: 8 Error Handling warnings by issuin
- Page 654 and 655: 8 Error Handling You must type the
- Page 656 and 657: 8 Error Handling s(2) ans = identif
- Page 658 and 659: 8 Error Handling end function f2(x)
- Page 660 and 661: 8 Error Handling Debugging Errors a
- Page 662 and 663: 9 Classes and Objects Classes and O
- Page 664 and 665: 9 Classes and Objects The diagram s
- Page 666 and 667: 9 Classes and Objects functions do
- Page 668 and 669: 9 Classes and Objects visible only
- Page 670 and 671: 9 Classes and Objects Class Method
- Page 672 and 673: 9 Classes and Objects Examples of C
- Page 674 and 675: 9 Classes and Objects not support p
- Page 676 and 677: 9 Classes and Objects overloaded me
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