Set Excel cell interior color to RGB value
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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'
0 Kommentare
Akzeptierte Antwort
Friedrich
am 3 Mai 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 Kommentare
Friedrich
am 3 Mai 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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!