Precision in writetable()

220 views (last 30 days)
madhan ravi
madhan ravi on 26 Oct 2020
Edited: Johannes Kalliauer on 20 Jul 2022
Is there a way to use writetable() to export using only 2 decimals? To be able to directly export it to an excel file.

Accepted Answer

dpb on 26 Oct 2020
Sadly, no. A OutputFormat formatting string or NumericPrecision option is not available.
I presume since a table can contain any data type including cells too many possibilities are opened up that TMW hasn't wanted to venture down that road.
Does seem like worthy of some design consideration for the normal cases, though, granted...
For text files, the numeric format is long g.
dpb on 10 Mar 2022
Because having rounded the numbers, they still are doubles and will have machine-precision rounding that Excel will try to preserve.
All you can do is format them inside Excel; same way as MATLAB, Excel keeps everything as a double internally; it only changes how they're displayed.
The second route writes the numeric value as text in the cell which is the source of the warning highlight.
There are several user-contributed utilities to allow one to set Excel table properties on the FEX. I believe it was @Image Analyst who wrote and posted a pretty nice starter set of Excel_utils that you may search for here on Answers -- I recall making a couple extensions and adding/posting another feature or two. That's been with the year...just at the moment I've got things in a state it isn't convenient to try to go find, but will try to get back...

Sign in to comment.

More Answers (1)

Johannes Kalliauer
Johannes Kalliauer on 20 Jul 2022
Edited: Johannes Kalliauer on 20 Jul 2022
  1 Comment
dpb on 20 Jul 2022
Doesn't get OP directly to Excel as per request, though...but correct that it does allow the formatting string.

Sign in to comment.


Find more on Data Type Conversion 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!