How to format cell font size, font color, and alignment in Excel from Matlab GUI program
Show older comments
Dear Matlab users,
I wrote these lines to export some data from my Matlab GUI to an Excel sheet. But, I need to do the following:
- control the font size and color.
- control the cell alignment.
I've read a lot of generous explanations that discuss a lot of issues, but could not find an answer to what I simply need.
Here is the code. Would you tell me what to add and where exactly?
header={'Radar Set', 'Antenna Height' 'Tilting Angle', 'Target Type', 'Long', 'Lat', 'Elevation', 'Max. Range', 'Area', 'Date', 'Time'};
xlswrite('SavedData.xlsx',header);
Data = {radar_set, get(handles.ant_height, 'String'), get(handles.tilt_ang, 'String'), target, get(handles.long,'string'), get(handles.lat,'string'), get(handles.alt,'string'), get(handles.maxrange,'string'), get(handles.area,'string'), datestr(clock, 'dd/mm/YYYY'), datestr(clock, 'HH:MM:SS')};
[number, strings, row] = xlsread('SavedData.xlsx');
lastRow = size(row,1);
nextRow = lastRow+1;
cellReference = sprintf('A%d', nextRow);
xlswrite('SavedData.xlsx', Data, 'Sheet1', cellReference);
Accepted Answer
More Answers (1)
Image Analyst
on 27 Jul 2024
Edited: Image Analyst
on 27 Jul 2024
I'm attaching a class, Excel_utils.m, that has all kinds of functions for formatting all kinds of things in Excel. I use it a lot. It also does things like find the first blank row in a column, etc.
Alternatively, if your data always goes into the same locations, you can just make a template workbook with all the custom formatting you want and save it to disk. Then make a copy of it in your code. Then write to the new copy you just made and it will have all the custom formatting you created.
I'm also attaching a demo on how to use ActiveX to open Excel, write stuff to it, and save it.
And a demo where it converts the row and column to the 'A1' cell reference that Excel prefers. For example row 3, column 27 would be 'AA3'.
These are the functions in the class file:
% Methods for class Excel_utils:
% Static methods:
%
% ActivateSheet DeleteEmptyExcelSheets FormatDecimalPlaces LeftAlignSheet
% AlignCells DeleteExcelSheets FormatLeftBorder WrapText
% AutoSizeAllSheets DuplicateExcelSheet FormatRightBorder
% CenterCellsAndAutoSizeColumns FormatBottomBorder GetNumberOfExcelSheets
% CenterCellsAndAutoSizeSpecificColumns FormatCellColor GoToNextRowInColumn
% ClearCells FormatCellFont InsertComments
% AutoSizeColumns
Each function also has a sample line of code on how to call the function. For example:
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------
% DeleteEmptyExcelSheets: deletes all empty sheets in the active workbook.
% This function loops through all sheets and deletes those sheets that are empty.
% Can be used to clean a newly created xls-file after all results have been saved in it.
% Sample call
% Excel = actxserver('Excel.Application');
% excelWorkbook = Excel.Workbooks.Open(excelFullFileName);
% Excel_utils.DeleteEmptyExcelSheets(Excel);
% Excel.ActiveWorkbook.Save;
% Excel.ActiveWorkbook.Close(false); % The 'false' argument prevents the popup window from showing, forcing the closure without user intervention.
% Excel.Quit;
% delete(Excel);
% clear('Excel')
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!