Matlab paint an excel cell

时光总嘲笑我的痴心妄想 提交于 2019-12-10 09:26:40

问题


can someone help me please in painting an excel cell through rgb in matlab? I want that the 10th cell will painted by rgb.

values{1}(1,:) = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '10'};
headers = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
xlswrite('example.xls', [headers; values{1}]);

thank you a lot :]


回答1:


You can color cells in an existing file with a procedure like this:

values = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '10'};
headers = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
rgb = [255 0 0]; %# if you have 0 to 1 values multiply by 255 and round
clr = rgb * [1 256 256^2]'; %'# convert to long number Excel understands

e = actxserver ('Excel.Application'); %# open Activex server
filename = fullfile(pwd,'example.xls'); %# full path required
if exist(filename,'file')
    ewb = e.Workbooks.Open(filename); %# open the file
else
    error('File does not exist.') %# or create a new file
end
esh = ewb.ActiveSheet;
for c = 1:numel(values)
    esh.Range(strcat(headers{c},values{c})).Interior.Color = clr;
end
ewb.Save
ewb.Close(false)
e.Quit

You can also specified RGB color with hex values and use hex2dec. In this case the order should be opposite, like 0000FF for red.



来源:https://stackoverflow.com/questions/10115265/matlab-paint-an-excel-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!