Categories
2. After the Advance

Matlab Import Data

Import is the MATLAB function which is used to import the data in different forms like txt, jpg, etc. Also, we can import the data from Clipboard using the import function. In many applications, we need various files or databases as input. These kinds of applications won’t work or operate without an import function. All types of input data is accessible in the import function. We can use text files, excel files, notepad, and also images in different formats. The basic syntax of the import function is ‘import data’. Along with import data, we can give the name of the file which we are going to use in our program.

Syntax:

There are following syntaxes to import the data into Matlab:

  • X = importdata( filename ), loads data into array X.
  • X = importdata( ‘-pastespecial’ ), loads data from the system clipboard not in the file.
  • X = importdata( ___, delimiterIn ), interprets delimiter Inas the column separator in ASCII file, filename, or the clipboard data. You can easily use delimiter In with any of the input arguments in the above syntaxes to import the data.

How to Create Matlab Import Data?

You can import data into MATLAB using different ways like

1. From a file

Do one of the following process to import data from a file:

  • Select Import Data on the Home tab, in the Variable section.
  • Double-click a file name to browser Folder.

2. From the clipboard

Do one of the following to import data from the clipboard:

  • Click on the Workspace browser title bar and then select Paste.
  • And Call import function with proper syntax.

Example to Implement Matlab Import Data

Below are the examples of Matlab Import Data:

Example 1: 

Let us see one example, in this example, we import an image and display it using the import data function. Basically in this example, we take that image name with extension take that name into a single inverted comma and assign it to the filename1. We import the data from that filename1 which basically stores the image. Then we can take a variable namely ‘ X’, in X we can store the imported data from filename1, for importing data we use an import data inbuilt function which is available on MATLAB. Then simply we display that image using image function, image function is used to display the image. We pass that variable which stores the imported data into image function to display the image.

Code:

clc ;
clear all ;
close all ;
X = importdata ( ' download.jpg');
Image(X)

Output:

Matlab Import Data Example 1

Explanation: As we seen the resultant image is the same as the original image. The resultant image is nothing but the imported data image which we store into the variable X.

After the example 1 we can see the  Import a Text File.

Example 2:

Let us see another example, in this example, we Import a Text File( mydoc01.txt) and Specify Delimiter and Column Header method “ using import data function. Basically, in this example, we take that filename with extension take that name into a single inverted comma and assign it to the filename1. We import the data from that filename1 which is basically stores the data. Then we can take a variable namely ‘ A ’, in A we can store the imported data from filename1, for importing data we use an import data inbuilt function which is available on MATLAB. Then simply we display that data, disp function is used to display the image. We pass that variable which stores the imported data into disp function to display the data

Code:

clc;
clear all;
close all;
filename = 'mydoc01.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename,delimiterIn,headerlinesIn);
for k = [3, 5] disp(A.colheaders{1, k})
disp(A.data[:,k])
disp(' ')
end

Output:

Matlab Import Data Example 2

 After the example 2, we can see the Import a Text File and Return Detected Delimiter method.

Example 3:

Let us see another example, in this example, we Import a Text File( mydoc01.txt) and Return Detected Delimiter method “ using import data function. Using a text editor, create a comma-delimited ASCII file called mydoc01.txt.

Code:

clc;
clear all;
close all;
filename = 'mydoc01.txt';
[A,delimiterOut]=importdata(filename)

Output:

Return Detected Delimiter Method Example 3

Leave a Reply

Your email address will not be published. Required fields are marked *