MATLAB: Set Excel cell interior color to RGB value

cellcolorexcelinteriorrgb

Hi,
In matlab I read a .BMP file of 20x20x3. Now I want to set the cell color of A1 of my workbook.xlsx, to the color of the first pixel.
% open image
a=imread('image1','bmp');
% Connect to Excel
Excel = actxserver('excel.application');
% Get Workbook object
WB = Excel.Workbooks.Open(fullfile(pwd, 'workbook1.xlsx'),0,false);
% Set the color of cell "A1" of Sheet 1 to RGB
WB.Worksheets.Item(1).Range('A2').Interior.Color = RGB(a(1,1,1),a(1,1,2),a(1,1,3));
% Save Workbook
WB.Save();
% Close Workbook
WB.Close();
% Quit Excel
Excel.Quit();
Well, this doesnt work. It works with interior.color = hex2dec('00FF00')
but not with RGB. How can I make this work? Error I get is: Undefined function or method 'RGB' for input arguments of type 'uint8'

Best Answer

Hi,
try the following:
rgb_val = @(r,g,b) r*1+g*256+b*256^2;
WB.Worksheets.Item(1).Range('A2').Interior.Color = rgb_val(a(1,1,1),a(1,1,2),a(1,1,3));
Hopefully MATLAB can resolve WB.Worksheets.Item(1).Range('A2').Interior.Color correctly. If not, do it in two steps:
interior = WB.Worksheets.Item(1).Range('A2').Interior;
interior.Color = rgb_val(a(1,1,1),a(1,1,2),a(1,1,3));