Write to Excel Sheet in Matlab



This article was originally written for http://www.codeproject.com. I am replicating the same article here. You can also read the same article here.

Introduction

This tutorial gives a complete overview as to how to write to Excel file in Matlab. Lot of times, we come across situations, where we need to save the data or read the data generated from the experimental simulation to an Excel file.


Steps

Step 1: The first step is to create a COM server which runs the Excel Application:

This assumes that the Excel Application is installed in your system.  If Excel is not installed, this statement will give an error. You can put the above statement between a try-catch block:


Step 2 :
In this step, we will check the version of Excel.


If the Microsoft Excel Version installed in your system is 2007, 2010 or above, you would get ExcelVersion would be 12 or above. In such a case, you can handle all the Excel workbook files with extensions *.xls, *.xlsx and *.xlsm. If the version is less than 12, only *.xls file would be supported.

Step 3 : In this step, we will open an Excel file “ResultFile.xls” in the current directory.


Step 4 :  By default, the visibility of the Excel file is set to FALSE. You can make the Excel file visible using the command:

 

Excel Sheet


Step 5 :
In this step, let us say, we will add some data  in the sheet titled “ExperimentSheet” and take the average of the data.

 

Let us assume, we are taking the experimental results from 10 different Experiments and we need to populate the results in Column A and Column B as shown in the figure. Finally, we need to take the Average of the Experimental Results using the Excel formula:


The first step is to open the  Excel Workbook and activate the ‘ExperimentSheet’ Sheet. The following code tries to open the ‘ExperimentSheet’ in ResultFile.xls. If the sheet is not found, it adds the Excel sheet and renames it to ‘ExperimentSheet’.



Add the Header


In order to write to an Excel File, we are using xlswrite() function. It has a number of advantages over the xlswrite function in Matlab. The xlswrite1 function can be downloaded from here. As seen from the Excel sheet, we need to first add two headers, “Experiment and Result” in A1 and  B1, respectively.

 

In order to make the header bold, we need to select the header:

 

In this step, we need to add the iteration number in the first column:

 


Similarly, we need to add the Experimental Results to Column B. Let us say, the experimental result is saved in the array ‘ExpResults’. For example, we can add the experimental results in the second column as,



Computing the Average

We need to add the row AVERAGE on 14th Row.

 

Entering the Average Formula


where xlcolumn() function converts between column name and number for Excel representation. The xlcolumn() module can be downloaded from here.

 

The last step is to save the Excel file and close the Excel application:

 

Download: The complete Matlab source can be downloaded from here.

 

 

 

About Kiran Chandrashekhar

Hey, Thanks for dropping by. My name is Kiran Chandrashekhar. I am the founder of SapnaEdu. I love Maths and Mathematical Shortcuts. Numbers fascinate me. I will be posting articles on Mathematical Shortcuts, Software Tips, Programming Tips in this website. I love teaching students preparing for various competitive examinations. Apart from SapnaEdu, I am the co-founder QLab Technologies . Read my complete story.
Tagged , , . Bookmark the permalink.






Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Protected by WP Anti Spam