MATLAB: How to read data from an Excel Spreadsheet using the Automation client support in MATLAB

automationexcelMATLABopenread

I would like to use the Automation client (COM/ActiveX) support in MATLAB to read data from an MS Excel Spreadsheet.

Best Answer

The following sample code illustrates how you can read data from an Excel File using the COM Automation support in MATLAB:
hExcel = actxserver('excel.application');
hExcel.Workbooks.Open('testxlsread.xls');
%BtchData is the name of the sheet:
data = hExcel.Worksheets.Item('BtchData').UsedRange.Value;
hExcel.Quit;
delete(hExcel)
The variable "data" will now store all the data that's read from Excel as a cell array.
You can then parse through to read the numeric and text data from the cell array:
for n = 1:r
for m = 1:c,
if ischar(data{n,m})
A(n,m) = NaN;
else A(n,m) = data{n,m};
data{n,m} = [];
end
end
end