Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Exporting to Audio and Video

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

For an example of writing video data to a file, see Example: Creating an AVI file.

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:

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. When 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 you then can play back using the movie function. You can export a MATLAB movie by saving it in MAT-file format, like any other MATLAB workspace variable. (However, only people running MATLAB can view your movie.) 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) format. AVI is a file format that allows animation and video clips to be played on a PC running Windows or The Open Group UNIX operating systems.

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. (For a complete list, see the avifile reference page.) 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)));
        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.

  3. Close the AVI file, using the close function.

    aviobj = close(aviobj);

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS