Main Content

importdata

Load data from file

Description

example

A= importdata(filename)loads data into arrayA.

example

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

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

example

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

example

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

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 classuint8because the helper function,imread, returns empty results forcolormapandalpha.

Using a text editor, create a space-delimited ASCII file with column headers calledmyfile01.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.

fork = [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 calledmyfile02.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 selectCopy.

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. Ifimportdatarecognizes the file extension, it calls the MATLAB helper function designed to import the associated file format (such asloadfor MAT-files orxlsreadfor spreadsheets). Otherwise,importdatainterprets the file as a delimited ASCII file.

For ASCII files and spreadsheets,importdataexpects 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

列分隔符, 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 specifyheaderlinesIn, theimportdatafunction 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,importdatacalls a helper function to read the data. When the helper function returns more than one nonempty output,importdatacombines the outputs into astructarray.

此表列出了file formats associated with helper functions that can return more than one output, and the possible fields in the structure array,A.

File Format Possible Fields Class

MAT-files

One field for each variable

Associated with each variable.

ASCII files and Spreadsheets

data
textdata
colheaders
rowheaders

For ASCII files,datacontains adoublearray. Other fields containcellarrays of character vectors.textdataincludes row and column headers.
For spreadsheets, each field contains astruct, with one field for each worksheet.

Images

cdata
colormap
alpha

Seeimread.

Audio files

data
fs

Seeaudioread.

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 inSupported File Formats for Import and Export.

If the ASCII file or spreadsheet contains either column or row headers, but not both,importdatareturns acolheadersorrowheadersfield in the output structure, where:

  • colheaderscontains only the last line of column header text.importdatastores all text in thetextdatafield.

  • rowheadersis 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, usereadtableinstead ofimportdata.

Version History

Introduced before R2006a