MATLAB Answers

Leon
0

How to set up writetable so that the generated Excel file follows a certain display format?

Asked by Leon
on 21 Aug 2019
Latest activity Commented on by Guillaume
on 11 Nov 2019 at 10:31
Below is my code to write some data into Excel:
T1 = table(Cruise, Station_no, Lon, Lat, Year, Month, Day, Time, Pressure, Depth);
writetable(T1, ['outputs/', app.FileName.Value,'.xlsx'])
The code works, but the format of the data inside the generated Excel file is kind of messy. Here are what I hope the generated Excel file will look like:
(a) The Longitude and latitude columns should be displayed in a format like %.4f. I do not want to trim any decimal digits off, because the precision of the data is important. All I want is to set up the display format. This is similar to the acton of clicking "Format cells" and then setting up the decial places inside Excel.
(b) Similarly, I want the pressure and depth columns to be formated like %.2f.
(c) All columns should be centered.
Is this even possible using Matlab writetable?

  0 Comments

Sign in to comment.

Tags

Products


Release

R2019a

1 Answer

Answer by Guillaume
on 21 Aug 2019
Edited by Guillaume
on 21 Aug 2019

Is this even possible using Matlab writetable?
No, writetable just put data in the spreadsheet cells and never touches any formatting, leaving it to the default. Note that xlswrite does the same.
My recommendation would be to create a template excel file (in excel) with the formatting you want, and every time you use writetable copy your template as a new xlsx file and use writetable to write into that file. The copying of the template can be done within matlab.
Another option is to use Excel COM interface from matlab to modify the formatting of the file after it's bee written by writetable but that's a lot more effort than the first option, and slower as well since the same file has to be opened twice (once by writetable once for your edit).

  3 Comments

Many thanks for the recommendations, Guillaume!
Could you give me an example of how to write that
an example of how to write that
"that" refers to what?

Sign in to comment.