How to round numbers in tables?

How can I round a number in a matlab table after it does its calculation? I have a cell that calculates the value to be 0.009999998 but I want it to read 0.01. I tried using format short g in the script but that didn't work.

Answers (3)

the cyclist
the cyclist on 22 Feb 2018
Edited: the cyclist on 22 Feb 2018
Use the round function.
You could have discovered this for yourself by typing
docsearch round
in the MATLAB command window, or typing
round matlab
into Google.
Note that there is a difference between the value that is calculated/stored, and how it is displayed. The format command is about the display.

2 Comments

I found the round function doesn't work with tables:
>> round(table(randn(3,4)), 3)
Error using round
First argument must be a numeric, logical, or char array.
So this answer was not helpful.
See my comment on your "answer" below.

Sign in to comment.

If you do not have the R2015a (I believe that is the correct release) that introduced the new round function, this anonymous function emulates it:
roundn = @(x,n) round(x .* 10.^n)./10.^n; % Round ‘x’ To ‘n’ Digits, Emulates Latest ‘round’ Function
x = 0.009999998;
xr = roundn(x,2)
xr =
0.01

4 Comments

How can I do this for table inputs? When I try to round(Table, 4) it doesn't work
How can I do this for table inputs? When I try to round(Table, 4) it doesn't work
neither does sin(Table) or Table + 5. You use round with tables the same way you use any other function with tables: by applying the function to table variables
yourtable.somevariable = round(yourtable.somevariable, 4);
If you want to apply it to all the table variables, then
yourtable = varfun(@(var) round(var, 4), yourtable)
@Guillaume — Thank you!
Lance Yarbrough
Lance Yarbrough on 14 Apr 2019
Edited: Lance Yarbrough on 14 Apr 2019
THANK YOU!! Looking for this answer forr most of the DAY.

Sign in to comment.

Is here a way to use the round function with tables?
Simply using round(my_table, 3) does not work:
t = array2table(randn(3, 4), 'VariableNames', {'A', 'B', 'C', 'D'}, ...
'RowNames', {'1', '2', '3'});
round(t, 3)
Output:
Error using round
First argument must be a numeric, logical, or char array.
I found this solution but it's quite convoluted:
t2 = t; t2{:, :} = round(t.Variables, 3)
Output:
t2 =
3×4 table
A B C D
______ ______ ______ ______
1 0.722 0.187 -0.439 -0.888
2 2.585 -0.082 -1.795 0.1
3 -0.667 -1.933 0.84 -0.545
Surely there is a better way...

3 Comments

tbl = table(randn(3,4))
tbl = 3×1 table
Var1 ________________________________________________ 0.77448 -0.91436 1.26 -2.1603 -0.098984 0.7149 -0.42872 -2.0906 0.84741 0.87568 -0.57396 0.39484
varfun(@(x)round(x,3),tbl)
ans = 3×1 table
Fun_Var1 ____________________________________ 0.774 -0.914 1.26 -2.16 -0.099 0.715 -0.429 -2.091 0.847 0.876 -0.574 0.395
Thanks but this doesn't preserve the row and column names, and possibly other attributes of the table.
I found the same question answered here by Steven Lord STAFF MVP.
OK. I can't tell from this comment whether you ended up with a solution that works for you.
If not, you might want to start a new question with your specific needs, rather than commenting and "answering" on an older question. You'll typically get more eyes on it if you do.

Sign in to comment.

Categories

Asked:

on 22 Feb 2018

Commented:

on 2 Oct 2021

Community Treasure Hunt

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

Start Hunting!