Is there a way to use writetable() to export using only 2 decimals? To be able to directly export it to an excel file.

 채택된 답변

dpb
dpb 2020년 10월 26일

4 개 추천

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.

댓글 수: 7

madhan ravi
madhan ravi 2020년 10월 26일
Ah :(, any suggestions dpb? Perhaps dlmwrite() to csv and readtable() and then writetable()?
dpb
dpb 2020년 10월 26일
What's the end objective file target and the data?
Perhaps rounding the data in the table first?
Walter Roberson
Walter Roberson 2020년 10월 26일
varfun() with appropriately selected input variables, with @(v) round(v,2) as the function to be applied.
madhan ravi
madhan ravi 2020년 10월 26일
Thank you dpb and sir Walter , rounding would be appropriate for my needs.
You can do this via
t = table( 1.12345, "test", 123.12345, 12.12, "12test")
t = 1×5 table
Var1 Var2 Var3 Var4 Var5 ______ ______ ______ _____ ________ 1.1235 "test" 123.12 12.12 "12test"
t{:,vartype('numeric') } = round( t{:,vartype('numeric')}, 3,'significant')
t = 1×5 table
Var1 Var2 Var3 Var4 Var5 ____ ______ ____ ____ ________ 1.12 "test" 123 12.1 "12test"
For me , Willingo's answer is still not working.
I tried and it did show numbers with 2 decimals in commond . but if I writetable() to excel, the numbers format is still long.
Also , I tried num2str first ,
num2str(1.345,'%.2')
and u will not only get the data with 2 decimals ,but also a green flag on the top left of the box.
dpb
dpb 2022년 3월 10일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Johannes Kalliauer
Johannes Kalliauer 2022년 7월 20일
편집: Johannes Kalliauer 2022년 7월 20일

0 개 추천

dlmwrite('yourfile.txt',t{:,:},'\t','precision','%10.2f')

댓글 수: 1

dpb
dpb 2022년 7월 20일
Doesn't get OP directly to Excel as per request, though...but correct that it does allow the formatting string.

댓글을 달려면 로그인하십시오.

Christine
Christine 2025년 2월 12일

0 개 추천

Another workaround might be executing this line for the variables inside the table.
variable = round(variable*100)/100;

댓글 수: 2

Use Y = round(X,N) to specify the number of decimal places.
Instead of
round(pi*100)/100
ans = 3.1400
use
round(pi,2)
ans = 3.1400
Another I've seen has been
str2double(sprintf('%.2f',pi))
ans = 3.1400

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2020b

태그

질문:

2020년 10월 26일

댓글:

dpb
2025년 4월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by