Filter löschen
Filter löschen

vector with floating point values

14 Ansichten (letzte 30 Tage)
Berbia
Berbia am 20 Jan. 2013
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 Kommentar
Roger Stafford
Roger Stafford am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Shashank Prasanna
Shashank Prasanna am 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]

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 20 Jan. 2013
Bearbeitet: Azzi Abdelmalek am 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 Kommentar
Berbia
Berbia am 20 Jan. 2013
Bearbeitet: Berbia am 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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by