How do I use FPRINTF to write numbers in exponential notation of variable exponent digits on a Windows computer in MATLAB?

조회 수: 316 (최근 30일)
In the official FPRINTF Help page, I observe that the way numbers are written in exponential notation differs depending on whether the computer on which MATLAB is running is a Windows machine or not (i.e., Linux, Solaris, or Mac). On Windows, a minimum of 3 exponential digits is printed for each number, whereas on everything else, a minimum of 2 is printed.
My application requires exponential notation with two digits in the exponent. I would like to be able to write my numeric arrays from MATLAB to a file where they are formatted in this way, on a Windows machine.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 2월 15일
편집: MathWorks Support Team 2021년 2월 17일
This change has been incorporated into the documentation begginning MATLAB 7.10 (R2010a). For the documentation of the latest release, refer to the following link:
For previous releases, read below for any additional information:
The following example code shows how to obtain UNIX-style exponential notation formatting on a Windows machine when writing a numeric array to a file with FPRINTF. The code makes use of SPRINTF and STRREP in the intermediate steps before writing to file:
A = [0.06 0.1 5 300]
A_str = sprintf('%e\t',A)
A_str = strrep(A_str, 'e+0','e+')
A_str = strrep(A_str, 'e-0','e-')
fid1 = fopen('file1.txt','w');
fprintf(fid1,'%s',A_str);
fclose(fid1);
It will be observed in the resulting text file that numbers in exponential notation with only two exponential digits are printed, instead of three.
To increase the number of digits in the exponent, the reverse procedure is required. The following adds two extra digits to the exponent:
A_str = strrep(A_str, 'e+0','e+000')
A_str = strrep(A_str, 'e-0','e-000')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품


릴리스

R2008a

Community Treasure Hunt

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

Start Hunting!

Translated by