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 data 24x5 14784 cell data_combined 1x5 2364 cell var_time 24x1 4608 cell Representing CDF Time Values CDF represents time differently than MATLAB. CDF represents date and time as the number of milliseconds since 1-Jan-0000. This is called an epoch in CDF terminology. MATLAB represents date and time as a serial date number, which is the number of days since 0-Jan-0000. To represent CDF dates, MATLAB uses an object called a CDF epoch object. To access the time information in a CDF object, use the object’s todatenum method. For example, this code extracts the date information from a CDF epoch object: 1 Extract the date information from the CDF epoch object returned in the cell array data (see “Importing Data from a CDF File” on page 7-3). Use the todatenum method of the CDF epoch object to get the date information, which is returned as a MATLAB serial date number. m_date = todatenum(data{1}); 2 View the MATLAB serial date number as a string. datestr(m_date) ans = 01-Jan-2001 Exporting Data to a CDF File To export data from the MATLAB workspace to a CDF file, use the cdfwrite function. Using this function, you can write variables and attributes to the file, specifying their names and associated values. See the cdfwrite reference page for more information. ThisexampleshowshowtowritedateinformationtoaCDFfile. Notehow the example uses the CDF epoch object constructor, cdfepoch, toconverta MATLAB serial date number into a CDF epoch. cdfwrite('myfile',{'Time_val',cdfepoch(now)}); 7-6

Common Data Format (CDF) Files You can convert a cdfepoch object back into a MATLAB serial date number with the todatenum function. 7-7

7 Working with Scientific Data Formats<br />

data 24x5 14784 cell<br />

data_combined 1x5 2364 cell<br />

var_time 24x1 4608 cell<br />

Representing CDF Time Values<br />

CDF represents time differently than <strong>MATLAB</strong>. CDF represents date and<br />

time as the number of milliseconds since 1-Jan-0000. This is called an epoch<br />

in CDF terminology. <strong>MATLAB</strong> represents date and time as a serial date<br />

number, which is the number of days since 0-Jan-0000. To represent CDF<br />

dates, <strong>MATLAB</strong> uses an object called a CDF epoch object. To access the time<br />

information in a CDF object, use the object’s todatenum method.<br />

For example, this code extracts the date information from a CDF epoch object:<br />

1 Extract the date information from the CDF epoch object returned in the<br />

cell array data (see “Importing Data from a CDF File” on page 7-3). Use<br />

the todatenum method of the CDF epoch object to get the date information,<br />

which is returned as a <strong>MATLAB</strong> serial date number.<br />

m_date = todatenum(data{1});<br />

2 View the <strong>MATLAB</strong> serial date number as a string.<br />

datestr(m_date)<br />

ans =<br />

01-Jan-2001<br />

Exporting Data to a CDF File<br />

To export data from the <strong>MATLAB</strong> workspace to a CDF file, use the cdfwrite<br />

function. Using this function, you can write variables and attributes to the<br />

file, specifying their names and associated values. See the cdfwrite reference<br />

page for more information.<br />

ThisexampleshowshowtowritedateinformationtoaCDFfile. Notehow<br />

the example uses the CDF epoch object constructor, cdfepoch, toconverta<br />

<strong>MATLAB</strong> serial date number into a CDF epoch.<br />

cdfwrite('myfile',{'Time_val',cdfepoch(now)});<br />

7-6

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

Saved successfully!

Ooh no, something went wrong!