Signifcant numbers set in a table
1 view (last 30 days)
Show older comments
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
Accepted Answer
Adam Danz
on 1 Feb 2021
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
0 Comments
More Answers (0)
See Also
Categories
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!