vector with floating point values

24 views (last 30 days)
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 Comment
Roger Stafford
Roger Stafford on 20 Jan 2013
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.

Sign in to comment.

Accepted Answer

Shashank Prasanna
Shashank Prasanna on 20 Jan 2013
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]

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 20 Jan 2013
Edited: Azzi Abdelmalek on 20 Jan 2013
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 Comment
Berbia
Berbia on 20 Jan 2013
Edited: Berbia on 20 Jan 2013
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!