Signifcant numbers set in a table

Can anyone please give an advice how to set the significant numbers only on the table numerical fields?
Code:
clc;
clear all;
xID = ['1';'2';'3';'4';'5'];
xName = {'Sun';'Moon';'Jupiter';'Earth';'Venus'};
I1 = [1.1112;9.2311245;3.341145;6.341122;54.211155];
I2 = [1.1199999;1.1211345;1.881188;1.5781188;1.63711777];
I3 = [6.232114;5.1211223654;4.452112272;3.5711235;2.62112247];
mt = table(xID, xName, I1, I2, I3);
table_data=cellfun(@(x) sprintf('%0.2f', x), mt), 'UniformOutput',0);

4 Comments

Stephen23
Stephen23 on 1 Feb 2021
Edited: Stephen23 on 1 Feb 2021
What is the goal: do you wish to change how the data are displayed (possibly without affecting how it is stored), or are you trying to change the numeric values themselves for some further calculations, or something else ... ?
If just display, I often use
format bank, format compact
for table data...it has pluses and minuses--integers are also displayed with the two decimal places being the most annoying.
There really is no good way to control display formatting in MATLAB command window, unfortunately.
Ali razi
Ali razi on 1 Feb 2021
Edited: Ali razi on 1 Feb 2021
I wish to show it using matlab report and not for display issues.
What, specifically, do you mean by " using matlab report"?

Sign in to comment.

 Accepted Answer

This example shows how to permanently round the data to 2dp which is usually undesirable but if you're printing it to a report, it's not like the level of precision can be edited anyway.
Use varfun to determine which columns are numeric.
colIsNum = varfun(@isnumeric, mt,'OutputFormat','uniform')
ans =
1×5 logical array
0 0 1 1 1
table_data = mt; % keep original, more precise data
table_data{:,colIsNum} = round(mt{:,colIsNum},2)
table_data =
5×5 table
xID xName I1 I2 I3
___ ___________ _____ ____ ____
1 {'Sun' } 1.11 1.12 6.23
2 {'Moon' } 9.23 1.12 5.12
3 {'Jupiter'} 3.34 1.88 4.45
4 {'Earth' } 6.34 1.58 3.57
5 {'Venus' } 54.21 1.64 2.62

More Answers (0)

Products

Release

R2020b

Asked:

on 1 Feb 2021

Answered:

on 1 Feb 2021

Community Treasure Hunt

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

Start Hunting!