Set Excel cell interior color to RGB value

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'

 Respuesta aceptada

Friedrich
Friedrich el 3 de Mayo de 2012
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));

7 comentarios

Eelco
Eelco el 3 de Mayo de 2012
Thanks for your help! It works partly. I dont get any errors, but no matter the values a(1,1,1) or a(150,150,1) or other; the cell A2 in excel is red. Some sort of weird red, but red.
Eelco
Eelco el 3 de Mayo de 2012
Well, it seems to work if I put it like this:
interior.Color = rgb_val(10,255,152);
But I want it to be the a(1,1,1) - a(1,1,3) values in stead of 10,255,152
Eelco
Eelco el 3 de Mayo de 2012
Everything seems to be (255,0,0) where as the color of the picture is (170,140,120)
Friedrich
Friedrich el 3 de Mayo de 2012
Yes, this is because your a is unit8 and the result of the rgb_val call is also in uint8 which is max of 255. Try:
rgb_val = @(r,g,b) double(r)*1+double(g)*256+double(b)*256^2;
Eelco
Eelco el 3 de Mayo de 2012
2007 SP2 English.
Eelco
Eelco el 3 de Mayo de 2012
works like a charm!
But why is it. can the color of a cell been set to:
"first value + (second value * 256) + (third value * 256 * 256)" ?
Friedrich
Friedrich el 3 de Mayo de 2012
Its all about the data types you use. MATLAB stays with the datatype you use in the computation. So when you combine uint8 types, the result will be uint8, e.g.
uint8(255)+10
This will give 255, because the maximum value of an unit8 is 255 and MATLAB cut off the rest.
So if your cell contains uint8, you will have the same behavior as without a cell.
So if you have a big matrix a with nx3 i would consider using bsxfun and sum in order to calculate the RGB Value Excel like to get:
a = uint8( [120 100 0
20 255 10]);
sum(bsxfun(@times,double(a),[1, 256, 256^2]),2)

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 3 de Mayo de 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by