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

495 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Feb 2021
Edited: MathWorks Support Team on 17 Feb 2021
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');
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')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!