MATLAB Answers

How can I format cell inputs when using writematrix?

3 views (last 30 days)
I am currently using a program that generates Excel files after doing a few signal processing operations and taking user input (via a GUI).
The data I am outputting is essnetially tabular data based on what has been done. The GUI creates an Excel File based on user input and then writes data to a named sheet, which is also taken from user input. The general writing I currently use looks like this:
% Calculations and what not come first, and are then followed by this code which uses the results to write to Excel.
% Most of the inputs are determined by the user via the GUI, hence the odd variable names (avoids app.blahblahblah.Value everywhere)
% The Range positioning was determined manually to create a desired output appearance.
% This is the equivalent of the Main Title for the data set. I would like this bolded, bigger font, and center aligned.
writematrix(DataName1,filename,'Sheet',sheetname,'Range','A1')
% These are primary parameters to differentiate parts of the data. I would like these bolded, underlined, and color coded.
writematrix(Para1,filename,'Sheet',sheetname,'Range','B1')
writematrix(Para2,filename,'Sheet',sheetname,'Range','C1')
writematrix(Para3,filename,'Sheet',sheetname,'Range','D1')
% These are the names of which individual bits of data are being recorded. These would only be bolded.
writematrix(Record1,filename,'Sheet',sheetname,'Range','A2')
writematrix(Record2,filename,'Sheet',sheetname,'Range','A3')
writematrix(Record3,filename,'Sheet',sheetname,'Range','A4')
writematrix(Record4,filename,'Sheet',sheetname,'Range','A5')
% A few other iterations of writematrix that use variables which do not need to be formatted.
I am not sure what the best way to apply this kind of formatting is, and would really appreciate some help!

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 Oct 2020
Correct. The new Excel functions do not allow formatting, and so I rarely use them. What's even worse is that if you start with a nicely formatted workbook with exactly the colors and fonts you want, and then use writematrix to write to it, it completely blows away all that nice formatting you made. I called them about this "bug" and they said it was working as designed (so far) but realized that it was a problem and put it on their feature request list.
They do offer some advantages in that they're faster because after the first write, they leave Excel open as a server so they don't need to spend time launching and shutting down Excel each time as happened with xlswrite() and xlsread().
There are two workarounds:
  1. You can use the old version, xlswrite(). You can even pre-format a workbook and xlswrite() will retain your formatting.
  2. You can use ActiveX to do the formatting. I'm attaching a demo and a class, Excel_utils, that has functions for setting fonts, cell shading colors, and borders.

  0 Comments

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 25 Oct 2020
writematrix() cannot do any formatting; neither can writecell() or writetable().
To do formatting in .xls and .xlsx you need to be using MS Windows with Excel installed, and use ActiveX (or .NET) to talk to Excel to construct the formating instructions.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by