What is MATLAB Data Import?

Importing data in MATLAB means loading facts from an external file. The importdata feature allows loading various data files of different formats. It has the following five forms –

S.N. Function and Description

A = importdata(filename)

Loads data into array A from the file denoted byfilename.


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.


A = importdata(___, delimiterIn, headerlinesIn)

Loads data from ASCII file, filename, or the clipboard, reading numeric data starting from lineheaderlinesIn+1.


[A, delimiterOut, headerlinesOut] = importdata(___)

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.

By default, Octave does not have support for importdata() function, so you will have to search and install this package to make following examples work with your Octave installation.

Example 1

Let us load and display an image file. Create a script file and type the following code in it:

When you run the file, MATLAB displays the image file. However, you must store it in the current directory.


Example 2

In this example, we import a text file and specify Delimiter and Column Header. let us create a space-delimited ASCII file with column headers, named weeklydata.txt.

Our text file weeklydata.txt looks like this –

Create a script file and type the following code in it –

When you run the file, it displays the following result –

Example 3

In this example, let us import data from clipboard.

Copy the following lines to the clipboard −

Mathematics is simple

Create a script file and type the following code –

When you run the file, it displays the following result –

Low-Degree File I/O

The import data function is a high-level function. The low-level file I/O functions in MATLAB allow the most control over reading or writing information to a document. however, those features need extra detailed information about your file to work efficiently.

MATLAB presents the following functions for read and write operations on the byte or character level –

Function Description
fclose Close one or all open files
feof Test for end-of-file
ferror Information about file I/O errors
fgetl Read line from file, removing newline characters
fgets Read line from file, keeping newline characters
fopen Open file, or obtain information about open files
fprintf Write data to text file
fread Read data from binary file
frewind Move file position indicator to beginning of open file
fscanf Read data from text file
fseek Move to specified position in file
ftell Position in open file
fwrite Write data to binary file

Import Text Data Files with Low-Level I/O

MATLAB gives the following features for low-level import of text information files −

  • The fscanf function reads formatted information in a text or ASCII file.
  • The fgetl and fgets functions read one line of a file at a time, where a newline character separates each line.
  • The fread function reads a move of information at the byte or bit level.


We have a text records file 'myfile.txt' saved in our working directory. The file stores rainfall information for three months; June, July and August for the year 2012.

The information in myfile.txt contains repeated sets of time, month and rainfall measurements at five places. The header data stores the number of months M; so we have M sets of measurements.

The file looks like this –

We will import information from this file and display this data. Take the following steps −

  • Open the file with fopen function and get the file identifier.
  • Describe the information in the record with format specifiers, which includes '%s' for a string, '%d' for an integer, or '%f' for a floating-point number.
  • To skip literal characters in the file, include them in the format description. To pass a data field, use an asterisk ('*') in the specifier.

For example, to read the headers and return the single value for M, we write:

  • By default, fscanf reads information according to our format description until it does not find any match for the data, or it reaches the end of the file. here we will use for loop for reading three units of data and each time, it'll read 7 rows and five columns.
  • We will create a structure named mydata in the workspace to store data read from the file. This structure has three fields - time, month, and raindata array.

Create a script file and type the following code in it –

When you run the file, it displays the following result –

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status