MATLAB Programming

MATLAB Programming MATLAB Programming

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

6 Data Import and Export Exporting Audio/Video Data MATLAB includes several functions that you can use to export audio or video data from the MATLAB workspace. These functions write audio data to a file using specific file formats. The following sections describe • “Exporting Audio Data” on page 6-94 • “Exporting Video Data in AVI Format” on page 6-94 This section also provides an example of writing video data to a file in “Example: Creating an AVI file” on page 6-95. Exporting Audio Data In MATLAB, audio data is simply numeric data that you can export using standard MATLAB data export functions, such as save. MATLAB also includes several functions that write audio data to files in specific file formats: • auwrite —Exports sound data in AU file format • wavwrite —Exports sound data in WAV file format Exporting Video Data in AVI Format You can export MATLAB video data as an Audio/Video Interleaved (AVI) file. To do this, you use the avifile function to create an avifile object. Once you have the object, you can use AVI file object methods and properties to control various aspects of the data export process. For example, in MATLAB, you can save a sequence of graphs as a movie that can then be played back using the movie function. You can export a MATLAB movie bysavingitinMAT-fileformat,likeanyotherMATLAB workspace variable. However, anyone who wants to view your movie must have MATLAB. (For more information about MATLAB movies, see the Animation section in the MATLAB Graphics documentation.) To export a sequence of MATLAB graphs in a format that does not require MATLAB for viewing, save the figures in Audio/Video Interleaved (AVI) 6-94

Working with Audio and Video Data format. AVI is a file format that allows animation and video clips to be played on a PC running Windows or on UNIX systems. Note To convert an existing MATLAB movie into an AVI file, use the movie2avi function. Example: Creating an AVI file To export a sequence of MATLAB graphs as an AVI format movie, perform these steps: 1 Create an AVI file object, using the avifile function. aviobj = avifile('mymovie.avi','fps',5); AVI file objects support properties that let you control various characteristics of the AVI movie, such as colormap, compression, and quality. (See the avifile reference page for a complete list.) avifile uses default values for all properties, unless you specify a value. The example sets the value of the frames per second (fps) property. 2 Capture the sequence of graphs and put them into the AVI file, using the addframe function. for k=1:25 h = plot(fft(eye(k+16))); set(h,'EraseMode','xor'); axis equal; frame = getframe(gca); aviobj = addframe(aviobj,frame); end The example uses a for loop to capture the series of graphs to be included in the movie. You typically use addframe to capture a sequence of graphs for AVI movies. However, because this particular MATLAB animation uses XOR graphics, you must call getframe to capture the graphs and then call addframe to add the captured frame to the movie. 6-95

Working with Audio and Video Data<br />

format. AVI is a file format that allows animation and video clips to be played<br />

on a PC running Windows or on UNIX systems.<br />

Note To convert an existing <strong>MATLAB</strong> movie into an AVI file, use the<br />

movie2avi function.<br />

Example: Creating an AVI file<br />

To export a sequence of <strong>MATLAB</strong> graphs as an AVI format movie, perform<br />

these steps:<br />

1 Create an AVI file object, using the avifile function.<br />

aviobj = avifile('mymovie.avi','fps',5);<br />

AVI file objects support properties that let you control various<br />

characteristics of the AVI movie, such as colormap, compression, and<br />

quality. (See the avifile reference page for a complete list.) avifile uses<br />

default values for all properties, unless you specify a value. The example<br />

sets the value of the frames per second (fps) property.<br />

2 Capture the sequence of graphs and put them into the AVI file, using the<br />

addframe function.<br />

for k=1:25<br />

h = plot(fft(eye(k+16)));<br />

set(h,'EraseMode','xor');<br />

axis equal;<br />

frame = getframe(gca);<br />

aviobj = addframe(aviobj,frame);<br />

end<br />

The example uses a for loop to capture the series of graphs to be included<br />

in the movie. You typically use addframe to capture a sequence of graphs<br />

for AVI movies. However, because this particular <strong>MATLAB</strong> animation uses<br />

XOR graphics, you must call getframe to capture the graphs and then call<br />

addframe to add the captured frame to the movie.<br />

6-95

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

Saved successfully!

Ooh no, something went wrong!