Hi everyone,
I need to print out in a txt file a list of numbers, one for each row, with this format:
i.dddddddddddddde+000 hence 1digit before decimal separator, 14 after that and 3 for the exponent.
Could anyone help me?
Thanks,
Stefano

2 commentaires

Harry Coules
Harry Coules le 13 Fév 2014
Modifié(e) : Harry Coules le 13 Fév 2014
If your list of numbers is a vector A, for example:
>> A = [pi;20*pi;300*pi;4000*pi;50000*pi]
A =
1.0e+05 *
0.0000
0.0006
0.0094
0.1257
1.5708
The following code will write a text file in the current directory in an output format similar to that which you specified, but with only 2 digits for the exponent:
fileID = fopen('filename.txt','w','n','UTF-8');
formatSpec = '%1.14e\n';
fprintf(fileID,formatSpec,A);
fclose(fileID);
I don't know if there's a 'clean' way to do three digits for the exponent... Does anyone else know?
Regards, Harry
dpb
dpb le 13 Fév 2014
Only if the actual field exponent is 3-digit in length will the C i/o formatting library display the third decimal. AFAIK this is C Standard behavior in that there's no formatting string option to specify the width of the exponent field.

Connectez-vous pour commenter.

 Réponse acceptée

Jos (10584)
Jos (10584) le 13 Fév 2014
Separate the mantissa and the exponent
Values = [pi ; 1.12301230123e17 ; 100000 ; 9999999999999]
E = floor(log10(Values)) % exponent
M = Values ./ (10.^E) % mantissa
TMP = [M(:) E(:)].'
fprintf('%1.14fe%03d\n', TMP)

Plus de réponses (1)

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by