필터 지우기
필터 지우기

Determining arbitrary precision from text

조회 수: 1 (최근 30일)
Edward
Edward 2012년 1월 20일
I am reading a number with arbitray precision, but I need to parse this into a string using a particular format.
Example 1: 1.015
Example 2: 60.15
Example 3: 700.150
In the first example, the correct string is CASE1_15. In the second example, the correct string is CASE60_15. In the last example, the correct string is CASE700_150
The code below accomplishes this, where the number has been stored in the fullcase column of matrix a, assuming a precision of 3 decimal places. Unfortunately, this requires me to know the precision beforehand. This means the code works for Examples 1 and 3, but fails for Example 2. If I alter code(replacing 1000 with 100), it fails for Examples 1 and 3, but works for Example 2. Isn't there some elegant means to detect the precision stored in that column so the code works no matter what the precision is?
strCase = num2str(floor(a(j,fullcase)));
strSubcase = num2str((a(j,fullcase)-floor(a(j,fullcase)))*1000);
fullCase = ['CASE',strCase,'_',strSubcase];

채택된 답변

Walter Roberson
Walter Roberson 2012년 1월 20일
Once you have converted to numeric form, you cannot determine the precision -- and the number actually stored probably isn't what you are expecting. Please see http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by