Matlab

How to Load Data from a File Using MATLAB’s importdata

During the development of many applications on MATLAB, we need to import data from a file which can easily be done by MATLAB’s built-in importdata() function. This function allows us to import data in different file formats such as docx, xlsx, txt, csv, jpg, and jpeg.

This guide is going to explore how to load data from a file using the importdata() function implementing different syntaxes using different examples.

Why Should We Need to Import Data?

Importing data is useful since it helps us improve the quality of our models. The reason is when we are able to import more data into our models, the training ability of our model could be improved. Further adding more data from useful resources will also help get a complete picture of our data and we should be able to see trends and patterns and will be able to make decisions based on these trends.

How to Load Data from a File Using the importdata() Function in MATLAB?

In MATLAB, we can easily and efficiently load data from a given file having a specified format using the importdata() function. This function takes some mandatory and optional inputs and displays the imported data on the command window.

Syntax

The syntaxes used for the importdata() function in MATLAB are given below.

A = importdata(filename)
A = importdata(___,delimiterIn)
A = importdata(___,delimiterIn,headerlinesIn)

 
Here:

The function A = importdata(filename) is responsible for loading data from the given file to an array A.

The function A = importdata(___,delimiterIn) is responsible for importing data delimiterIn with the previous input argument as a column separation from the ASCII file or the clipboard data.

The function A = importdata(___,delimiterIn,headerlinesIn) is responsible for loading data from the given ASCII file or clipboard reading numeric data that starts from headerlinesIn+1.

Example 1: How to Import and Display a Image Using importdata() Function?

This example imports and displays an image using the importdata() function in MATLAB.

A = importdata("Bar_Graph.jpg");
image(A)

 

Example 2: How to Load And Display Text File Specifying Delimiter Using importdata() Function?

In this example, we load and display data from the given text file by specifying a delimiter using the importdata() function in MATLAB.

A = importdata("cos_file.txt",'')

 

Example 3: How to Load And Display CSV File Specifying Delimiter and Column Header Using importdata() Function?

This MATLAB code imports and displays data from the given CSV file specifying delimiter and column header using the importdata() function in MATLAB.

A = importdata("file.csv",'',5)

 

Conclusion

Importing or loading data from a file is a widely used technique followed by MATLAB developers while developing applications. This task can be easily done utilizing the built-in importdata() function. This function can load data from docx, txt, xlsx, csv, and many other file formats. This guide has presented the workings of the importdata() function with different syntaxes and examples, allowing you to understand the main workings of the function.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.