vector with floating point values

21 次查看(过去 30 天)
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