MATLAB: How to obtain information about the background color of a cell in an Excel spreadsheet using MATLAB 7.8 (R2009a)

actxserverbackgroundcellcolorcomexcelMATLAB

I have an Excel spreadsheet in which the cells are assigned a specific background color. This colors serves as a code that dictates how the information in the cell is to be processed. I am able to read the data from the Excel sheet into MATLAB. I want to read the background color information of the cells too.

Best Answer

It is possible to retrieve the background color information of a cell of an Excel spreadsheet using the COM client interface. The code below indicates how this can be achieved:
E = actxserver('Excel.Application');
% Modify the path to include the FULL path to your Excel file
myExcelSheet = 'C:\work\myFile.xls';
E.Workbooks.Open(myExcelSheet);
% Cell whose background color information is required
myCell = 'C4';
Range = E.Range(myCell);
Range.Interior.Color;
cellColor = Range.Interior.Color;
% The color returned by Excel is 24-bit color containing 8-bits for each R, G and B component
% The MSB contains information about G-component and the LSB contains information about the R-component
cellColorBinary = dec2bin(cellColor, 24);
% These colors match the colors that are displayed when you inspect the R, G
% and B values when you open the Custom Color palette
colorB = bin2dec(cellColorBinary(1:8));
colorG = bin2dec(cellColorBinary(9:16));
colorR = bin2dec(cellColorBinary(17:24));