Read and Write Excel files

From ControlTheoryPro.com

Jump to: navigation, search
Symbol.gif
Read and Write Excel files
Green carrot left.gif
:Category:MATLAB Read and Write MATLAB
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.

1 Reading and Writing from an Excel spreadsheet

There are several reasons to read or write data from Excel. The most common reason is the need to share results or data with people who do not have MATLAB or are inexperienced at using MATLAB. One of the best reasons to read from Excel files is to get analysis or simulation parameters.

1.1 Reading from Excel files

The xlsread command is used to read data from a file into the current workspace.

  • Read all the data from the tab DataSheet in the file xlsData.xls
 >> [num, txt, raw] = xlsread('xlsData.xls', 'DataSheet');
  • num: numeric data in double array from the sheet DataSheet
  • txt: numeric data in array num and text data in cell array txt, all cells in txt that correspond to numeric data contain the empty string
  • raw: unprocessed cell contents in a cell array with both text and numeric data

1.2 Writing to Excel files

The xlswrite command is used to write data to an Excel file from data in the current workspace. Note that xlsread was not available prior to MATLAB version 7.

  • Write all data in cell array myData
 >> xlswrite('xlsData.xls', myData, 'DataSheet')
  • Write the data to the worksheet tab DataSheet starting at cell A1
 >> xlswrite('xlsData.xls', myData, 'DataSheet', 'A1')