www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/WriteMATLABDatatoHDF4FileExample.m

    %% Write MATLAB Data to HDF4 File  
% This example shows how to write MATLAB(R) arrays to a Scientific Data
% Set in an HDF4 file.   

%% Add Package to Import List 
% Add the |matlab.io.hdf4.*| path to the import list. 
import matlab.io.hdf4.* 

%%
% Prefix subsequent calls to functions in the |matlat.io.hdf4.sd| package
% with |sd|, rather than the entire package path.  

%% Create HDF4 File 
% Create a new HDF4 file using the |matlab.io.hdf4.sd.start| function. This
% function corresponds to the SD API routine, |SDstart|. 
sdID = sd.start('mydata.hdf','create'); 

%%
% |sd.start| creates the file and returns a file identifier named |sdID|. 

%%
% To open an existing file instead of creating a new one, call |sd.start|
% with |'write'| access instead of |'create'|.  

%% Create HDF4 Data Set 
% Create a data set in the file for each MATLAB array you want to export.
% If you are writing to an existing data set, you can skip ahead to the
% next step. In this example, create one data set for the array of sample
% data, |A|, using the |matlab.io.hdf4.sd.create| function. This function
% corresponds to the SD API routine, |SDcreate|. The |ds_type| argument
% is a string specifying the MATLAB data type of the data set. 
A = [1 2 3 4 5 ; 6 7 8 9 10 ; 11 12 13 14 15]; 
ds_name = 'A';
ds_type = 'double';
ds_dims = size(A);
sdsID = sd.create(sdID,ds_name,ds_type,ds_dims); 

%%
% |sd.create| returns an HDF4 SD data set identifier, |sdsID|.  

%% Write MATLAB Data to HDF4 File 
% Write data in |A| to the data set in the file using the |matlab.io.hdf4.sd.writedata|
% function. This function corresponds to the SD API routine, |SDwritedata|.
% The |start| argument specifies the zero-based starting index. 
start = [0 0];
sd.writeData(sdsID,start,A); 

%%
% |sd.writeData| queues the write operation. Queued operations execute when
% you close the HDF4 file.  

%% Write MATLAB Data to Portion of Data Set 
% Replace the second row of the data set with the vector |B|. Use a |start|
% input value of |[1 0]| to begin writing at the second row, first column.
% |start| uses zero-based indexing. 
B = [9 9 9 9 9];
start = [1 0];
sd.writeData(sdsID,start,B);  

%% Write Metadata to HDF4 File 
% Create a global attribute named |creation_date|, with a value that is
% the current date and time. Use the |matlab.io.hdf4.sd.setAttr| function,
% which corresponds to the SD API routine, |SDsetattr|. 
sd.setAttr(sdID,'creation_date',datestr(now)); 

%%
% |sd.Attr| creates a file attribute, also called a global attribute, associated
% with the HDF4 file identified by |sdID|.  

%% 
% Associate a predefined attribute, |cordsys|, to the data set identified
% by |sdsID|. Possible values of this attribute include the text strings
% |'cartesian'|, |'polar'|, and |'spherical'|. 
attr_name = 'cordsys';
attr_value = 'polar';
sd.setAttr(sdsID,attr_name,attr_value);  

%% Close HDF4 Data Set 
% Close access to the data set, using the |matlab.io.hdf4.sd.endAccess|
% function. This function corresponds to the SD API routine, |SDendaccess|.
% You must close access to all the data sets in and HDF4 file before closing
% the file. 
sd.endAccess(sdsID);  

%% Close HDF4 File 
% Close the HDF4 file using the |matlab.io.hdf4.sd.close| function. This
% function corresponds to the SD API routine, |SDend|. 
sd.close(sdID); 

%%
% Closing an HDF4 file executes all the write operations that have been
% queued using |SDwritedata|.