필터 지우기
필터 지우기

vector with floating point values

조회 수: 14 (최근 30일)
Berbia
Berbia 2013년 1월 20일
I have three floating point variables, say, A,B,C. I wrote these variables in the file as
fprintf(fid,'%12.5f %12.5f %12.5f\n',A,B,C);
Is this possible to concatenate these variables a row vector, X=[A B C] and the values should be floating point with 5 digits after decimal points.
  댓글 수: 1
Roger Stafford
Roger Stafford 2013년 1월 20일
It is important to realize the distinction between the way a double precision floating number is displayed and the number that is actually used within the computer for computations. The internal number is not decimal - it is represented in binary with 53-bit precision and this is not subject to adjustment (unless you convert to single precision.) Matlab's 'format' function will adjust the nature of the display of this value. For example, the number pi will be displayed as 3.1416 with 'format short' but 3.14159265... with 'format long', but that doesn't alter the internal number. It is the same either way. The same applies to numbers as displayed by 'fprintf'. In your example you are displaying A, B, and C with five decimal places after the decimal point, but however you display them, that has no effect on the actual values of A, B, and C as used in computations. In other words there is no meaning to the statement you made, "the values should be floating point with 5 digits after decimal points", as applied to the numbers being used in computations.

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

채택된 답변

Shashank Prasanna
Shashank Prasanna 2013년 1월 20일
The default display format is 'short' Since you already have data with 5 decimal place in your file, you can just read it and adjust the display format such that it shows all 5 precisions:
format longg
A = 1.12345;
B = 2.12345
C = 3.12345
X = [A B C]

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 20일
편집: Azzi Abdelmalek 2013년 1월 20일
Yes you can
fid=fopen('filename.txt','w')
A=1.2345678
B=2.30000004
C=3.42451478
X=[A B C]
fprintf(fid,'%12.5f %12.5f %12.5f\n',X)
fclose(fid)
  댓글 수: 1
Berbia
Berbia 2013년 1월 20일
편집: Berbia 2013년 1월 20일
I wont need to write these variables in file I just need to concatenate it. If I use X=[A B C] each variables have default 4 precision how to specify the decimal values with 5 precision

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by