Main Content

importdata

Load data from file

Description

A = importdata(filename) loads data into array A.

example

A = importdata('-pastespecial') loads data from the system clipboard rather than from a file.

example

A = importdata(___,delimiterIn) interprets delimiterIn as the column separator in ASCII file, filename, or the clipboard data. You can use delimiterIn with any of the input arguments in the above syntaxes.

A = importdata(___,delimiterIn,headerlinesIn) loads data from ASCII file, filename, or the clipboard, reading numeric data starting from line headerlinesIn+1.

example

[A,delimiterOut,headerlinesOut] = importdata(___) additionally returns the detected delimiter character for the input ASCII file in delimiterOut and the detected number of header lines in headerlinesOut, using any of the input arguments in the previous syntaxes.

example

Examples

collapse all

Import and display the sample image, ngc6543a.jpg.

A = importdata('ngc6543a.jpg');
image(A)

Figure contains an axes object. The axes object contains an object of type image.

The output, A, is class uint8 because the helper function, imread, returns empty results for colormap and alpha.

Using a text editor, create a space-delimited ASCII file with column headers called myfile01.txt.

Day1  Day2  Day3  Day4  Day5  Day6  Day7
95.01 76.21 61.54 40.57  5.79 20.28  1.53
23.11 45.65 79.19 93.55 35.29 19.87 74.68
60.68  1.85 92.18 91.69 81.32 60.38 44.51
48.60 82.14 73.82 41.03  0.99 27.22 93.18
89.13 44.47 17.63 89.36 13.89 19.88 46.60

Import the file, specifying the space delimiter and the single column header.

filename = 'myfile01.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename,delimiterIn,headerlinesIn);

View columns 3 and 5.

for k = [3, 5]
   disp(A.colheaders{1, k})
   disp(A.data(:, k))
   disp(' ')
end
Day3
   61.5400
   79.1900
   92.1800
   73.8200
   17.6300

 
Day5
    5.7900
   35.2900
   81.3200
    0.9900
   13.8900

Using a text editor, create a comma-delimited ASCII file called myfile02.txt.

1,2,3
4,5,6
7,8,9

Import the file, and display the output data and detected delimiter character.

filename = 'myfile02.txt';
[A,delimiterOut]=importdata(filename)
A =

     1     2     3
     4     5     6
     7     8     9


delimiterOut =

,

Copy the following lines to the clipboard. Select the text, right-click, and then select Copy.

1,2,3
4,5,6
7,8,9

Import the clipboard data into MATLAB® by typing the following.

A = importdata('-pastespecial')
A =

     1     2     3
     4     5     6
     7     8     9

Input Arguments

collapse all

Name and extension of the file to import, specified as a character vector or a string scalar. If importdata recognizes the file extension, it calls the MATLAB helper function designed to import the associated file format (such as load for MAT-files or xlsread for spreadsheets). Otherwise, importdata interprets the file as a delimited ASCII file.

For ASCII files and spreadsheets, importdata expects to find numeric data in a rectangular form (that is, like a matrix). Text headers can appear above or to the left of the numeric data, as follows:

  • Column headers or file description text at the top of the file, above the numeric data.

  • Row headers to the left of the numeric data.

Example: 'myFile.jpg'

Data Types: char | string

Column separator character, specified as a character vector or a string scalar. The default character is interpreted from the file. Use '\t' for tab.

Example: ','

Example: ' '

Data Types: char | string

Number of text header lines in the ASCII file, specified as a nonnegative scalar integer. If you do not specify headerlinesIn, the importdata function detects this value in the file.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Data from the file, returned as a matrix, multidimensional array, or scalar structure array, depending on the characteristics of the file. Based on the file format of the input file, importdata calls a helper function to read the data. When the helper function returns more than one nonempty output, importdata combines the outputs into a struct array.

This table lists the file formats associated with helper functions that can return more than one output, and the possible fields in the structure array, A.

File FormatPossible FieldsClass

MAT-files

One field for each variable

Associated with each variable.

ASCII files and Spreadsheets

data
textdata
colheaders
rowheaders

For ASCII files, data contains a double array. Other fields contain cell arrays of character vectors. textdata includes row and column headers.
For spreadsheets, each field contains a struct, with one field for each worksheet.

Images

cdata
colormap
alpha

See imread.

Audio files

data
fs

See audioread.

The MATLAB helper functions for most other supported file formats return one output. For more information about the class of each output, see the functions listed in Supported File Formats for Import and Export.

If the ASCII file or spreadsheet contains either column or row headers, but not both, importdata returns a colheaders or rowheaders field in the output structure, where:

  • colheaders contains only the last line of column header text. importdata stores all text in the textdata field.

  • rowheaders is created only when the file or worksheet contains a single column of row headers.

Detected column separator in the input ASCII file, returned as a character vector.

Detected number of text header lines in the input ASCII file, returned as an integer.

Tips

  • To import ASCII files with nonnumeric characters outside of column or row headers, including columns of character data or formatted dates or times, use readtable instead of importdata.

Version History

Introduced before R2006a