Importing Excel Workbook/WorkSheet (XLS files) in MATLAB

At times we need to import Excel files in MATLAB, as they might be our data source. The data structure of XLS files are much like MATLAB itself, so MATLAB has got functionality for a direct import the content of these files as a variable.

In this example we have created a file named matlab.xls, which have got the following content, it contains data in sheet one only (Although these methods will be applicable for multiple sheets even).

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


A 6x6 matrix.

Now we will look at how to import this in MATLAB.



1. Drag and Drop in MATLAB's Workspace
Just drag and drop your Excel (XLS) file in MATLAB's workspace, and you will see an import window appearing in front of you. There you will get many options, that will let you choose how to import this data.

Importing XLS (Excel) file in MATLAB
Importing XLS (Excel) file in MATLAB
2. Through IMPORT DATA button in newer MATLAB versions
Click on Import Data button, you will get a pop up window to browse your file to get imported.
Importing XLS (Excel) file in MATLAB
Importing XLS (Excel) file in MATLAB
 3. Through uiimport command 

Us the command like : uiimport('C:\Users\x\Desktop\matlab.xls') 
You will get a window popup so that you can select the data which you want to import..

4. Using xlsread command
 Use the command like: >> xlsread('C:\Users\x\Desktop\matlab.xls', 'Sheet1');
or in general,
xlsread(filename, sheetname);

This function also let you to separate numeric data & string data using the syntax.[ndata,sdata]=xlsread(filename, sheetname);
The numeric data will be saved in the first variable ‘ndata’.
The strings will be saved in the second variable ‘sdata’.

0 comments: