MATLAB: Use matlab write data to excel

xlswrite

Thanks in advance, any comments will be appreciate! I have data that I have write to a cell in an Excel Spreadsheet, but I also want to assign a background color and font color to this cell. I would like to know the way to do this through MATLAB. How can I do?

Best Answer

You need to use ActiveX programming. Search for ActiveX because there are lots of examples in this forum. For example:
function FormatCellFont(Excel, sheetNumber, cellsToFormat)
try
numberOfCells = size(cellsToFormat, 1);
worksheets = Excel.sheets;
% thisSheet = get(worksheets, 'Item', sheetNumber);
thisSheet = Excel.ActiveSheet;
thisSheetsName = Excel.ActiveSheet.Name; % For info only.
for k = 1 : numberOfCells
row = cellsToFormat(k, 1); % Get the row number.
column = cellsToFormat(k, 2); % Get the column number.
columnLetterCode = cell2mat(ExcelCol(column));
% Get a reference to the cell at this row in column A.
cellReference = sprintf('%s%d', columnLetterCode, row);
theCell = thisSheet.Range(cellReference);
% Set the horizontal alignment to left justified.
theCell.HorizontalAlignment = 2;
% Set the font.
theCell.Font.Name = 'Calibri';
% Set the font to bold.
theCell.Font.Bold = true;
% Set the font size to 12 points.
theCell.Font.Size = 12;
% Set the font color to blue.
theCell.Font.Color = -65536;
end
catch ME
errorMessage = sprintf('Error in function FormatCellFont.\n\nError Message:\n%s', ME.message);
fprintf(errorMessage);
WarnUser(errorMessage);
end
return; % from FormatCellFont