Effacer les filtres
Effacer les filtres

vector with floating point values

14 vues (au cours des 30 derniers jours)
Berbia
Berbia le 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 commentaire
Roger Stafford
Roger Stafford le 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.

Connectez-vous pour commenter.

Réponse acceptée

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

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 20 Jan 2013
Modifié(e) : Azzi Abdelmalek le 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 commentaire
Berbia
Berbia le 20 Jan 2013
Modifié(e) : Berbia le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by