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

Harry Coules
Harry Coules 2014년 2월 13일
편집: Harry Coules 2014년 2월 13일
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 2014년 2월 13일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jos (10584)
Jos (10584) 2014년 2월 13일

0 개 추천

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)

추가 답변 (1개)

Stefano
Stefano 2014년 2월 13일

0 개 추천

thank you guys!

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

질문:

2014년 2월 13일

답변:

2014년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by