Exact number of digits output in num2str

조회 수: 102 (최근 30일)
Bob Roos
Bob Roos 2015년 6월 23일
답변: Bob Roos 2015년 6월 23일
I am building input files for Nastran and it requires exact spacing in the strings for the numbers (in string format). I am doing a lot of runs so I don't want to change my Matlab code all the time. I need to output some values with exactly 4 characters (so 1 becomes 1.00, pi becomes 3.14, 0.5 becomes 0.50, etc), but I haven't been able to figure it out yet. Using the minimum number of characters in num2str with '%2f' and rounding beforehand using round(100*n)/100 yields for pi 3.140000, probably due to roundoff error. Any help?

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2015년 6월 23일
Well you get part of the way with %4.2f, as in
num2str(pi,'%4.2f')
that breaks down for large numbers alike pi*1e12, but then I don't know how you're supposed to squeeze that into exactly 4 chars...
HTH

추가 답변 (1개)

Bob Roos
Bob Roos 2015년 6월 23일
Thanks, didn't know that you could put the 4. there as well.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by