print numbers in Enginering Notation
Mostrar comentarios más antiguos
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 comentarios
Harry Coules
el 13 de Feb. de 2014
Editada: Harry Coules
el 13 de Feb. de 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
el 13 de Feb. de 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.
Respuesta aceptada
Más respuestas (1)
Stefano
el 13 de Feb. de 2014
0 votos
Categorías
Más información sobre Low-Level File I/O en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!