MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 482 and 483: 6 Data Import and Export Ifthefilet
- Page 484 and 485: 6 Data Import and Export For exampl
- Page 486 and 487: 6 Data Import and Export m = memmap
- Page 488 and 489: 6 Data Import and Export Mapping of
- Page 490 and 491: 6 Data Import and Export Repeating
- Page 492 and 493: 6 Data Import and Export Note To su
- Page 494 and 495: 6 Data Import and Export d = m.Data
- Page 496 and 497: 6 Data Import and Export ans = 166x
- Page 498 and 499: 6 Data Import and Export If you hav
- Page 500 and 501: 6 Data Import and Export m.Data(60)
- Page 502 and 503: 6 Data Import and Export m = memmap
- Page 504 and 505: 6 Data Import and Export d = m.Data
- Page 506 and 507: 6 Data Import and Export Data: 1658
- Page 508 and 509: 6 Data Import and Export One MATLAB
- Page 510 and 511: 6 Data Import and Export disp('ANSW
- Page 512 and 513: 6 Data Import and Export Terminatin
- Page 514 and 515: 6 Data Import and Export character.
- Page 516 and 517: 6 Data Import and Export Table6-2 A
- Page 518 and 519: 6 Data Import and Export A = dlmrea
- Page 520 and 521: 6 Data Import and Export number of
- Page 522 and 523: 6 Data Import and Export Exporting
- Page 524 and 525: 6 Data Import and Export A = [ 1 2
- Page 526 and 527: 6 Data Import and Export diary off
- Page 528 and 529: 6 Data Import and Export Importing
- Page 530 and 531: 6 Data Import and Export Working wi
- Page 534 and 535: 6 Data Import and Export 3 Close th
- Page 536 and 537: 6 Data Import and Export type = Mic
- Page 538 and 539: 6 Data Import and Export ndata = xl
- Page 540 and 541: 6 Data Import and Export 41 42 43 4
- Page 542 and 543: 6 Data Import and Export Note While
- Page 544 and 545: 6 Data Import and Export Use these
- Page 546 and 547: 6 Data Import and Export Writing Bi
- Page 548 and 549: 6 Data Import and Export The act of
- Page 550 and 551: 6 Data Import and Export Conversion
- Page 552 and 553: 6 Data Import and Export • Two sp
- Page 554 and 555: 6 Data Import and Export Exchanging
- Page 556 and 557: 6 Data Import and Export urlread('h
- Page 558 and 559: 6 Data Import and Export Once you h
- Page 560 and 561: 6 Data Import and Export For exampl
- Page 562 and 563: 6 Data Import and Export 6-124
- Page 564 and 565: 7 Working with Scientific Data Form
- Page 566 and 567: 7 Working with Scientific Data Form
- Page 568 and 569: 7 Working with Scientific Data Form
- Page 570 and 571: 7 Working with Scientific Data Form
- Page 572 and 573: 7 Working with Scientific Data Form
- Page 574 and 575: 7 Working with Scientific Data Form
- Page 576 and 577: 7 Working with Scientific Data Form
- Page 578 and 579: 7 Working with Scientific Data Form
- Page 580 and 581: 7 Working with Scientific Data Form
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