Main Content

Import Images, Audio, and Video Interactively

Import data interactively into MATLAB® workspace.

Note

For information on importing text files, see Read Text File Data Using Import Tool. For information on importing spreadsheets, see Read Spreadsheet Data Using Import Tool.

Viewing the Contents of a File

Start the Import Wizard by clicking the Import Data button or calling uiimport.

To view images or video, or to listen to audio, click the < Back button on the first window that the Import Wizard displays.

Import Wizard with mouse pointer on < Back button

The right pane of the new window includes a preview tab. Click the button in the preview tab to show an image or to play audio or video.

Import Wizard preview tab

Specifying Variables

The Import Wizard generates default variable names based on the format and content of your data. You can change the variables in any of the following ways:

The default variable name for data imported from the system clipboard is A_pastespecial.

If the Import Wizard detects a single variable in a file, the default variable name is the file name. Otherwise, the Import Wizard uses default variable names that correspond to the output fields of the importdata function. For more information on the output fields, see the importdata function reference page.

Renaming or Deselecting Variables

To override the default variable name, select the name and type a new one.

Renaming variable R in Import Wizard

To avoid importing a particular variable, clear the check box in the Import column.

Importing to a Structure Array

To import data into fields of a structure array rather than as individual variables, start the Import Wizard by calling uiimport with an output argument. For example, the sample file durer.mat contains three variables: X, caption, and map. If you issue the command

durerStruct = uiimport('durer.mat')

and click the Finish button, the Import Wizard returns a scalar structure with three fields:

durerStruct = 
          X: [648x509 double]
        map: [128x3 double]
    caption: [2x28 char]

To access a particular field, use dot notation. For example, view the caption field:

disp(durerStruct.caption)

MATLAB returns:

Albrecht Durer's Melancolia.
Can you find the matrix?  

For more information, see Structure Arrays.

Generating Reusable MATLAB Code

To create a function that reads similar files without restarting the Import Wizard, select the Generate MATLAB code check box. When you click Finish to complete the initial import operation, MATLAB opens an Editor window that contains an unsaved function. The default function name is importfile.m or importfileN.m, where N is an integer.

The function in the generated code includes the following features:

  • For text files, if you request vectors from rows or columns, the generated code also returns vectors.

  • When importing from files, the function includes an input argument for the name of the file to import, fileToRead1.

  • When importing into a structure array, the function includes an output argument for the name of the structure, newData1.

However, the generated code has the following limitations:

  • If you rename or deselect any variables in the Import Wizard, the generated code does not reflect those changes.

  • If you do not import into a structure array, the generated function creates variables in the base workspace. If you plan to call the generated function from within your own function, your function cannot access these variables. To allow your function to access the data, start the Import Wizard by calling uiimport with an output argument. Call the generated function with an output argument to create a structure array in the workspace of your function.

MATLAB does not automatically save the function. To save the file, select Save. For best results, use the function name with a .m extension for the file name.

See Also

| |

Related Topics