Normalizing the E descriptor

7 views (last 30 days)
I've created data files with Matlab using the E descriptor for the number format. This produces output of the form:
However, a Fortran90 based code in which I need to load the data can't interpret this format. It expects to see numbers that are "Normalized," that is, the number is shifted over so that the first digit is in the tenths position. A leading zero and decimal are always present and a minus sign, if needed. For example, our above numbers need to be in the form:
I've been trying to get the data into this format and have managed to do so in a script by converting the numbers to strings, doing the logic, and returning a cell of strings. However I am unable to write this data to a file without losing my format. Is there a simpler way to normalize the number format? Is there a way to write a cell array of strings to a regular file?

Accepted Answer

Star Strider
Star Strider on 13 Aug 2012
This does essentially what you want it to do. I didn't put it in a neat ‘function’ form because I'm not certain how you want to use it:
x = [pi; 10*pi; 100*pi; eps; -eps; 1/eps; realmax/10; realmin];
for k1 = 1:length(x)
if abs(x(k1)) >= 1
xpnt(k1) = fix(log10(abs(x(k1))))+1;
x1(k1) = x(k1)./(10.^(xpnt(k1)));
elseif abs(x(k1)) < 1
xpnt(k1) = fix(log10(abs(x(k1))));
x1(k1) = x(k1)./(10.^(xpnt(k1)));
fprintf(1,'\n%18.15fE%0+4d\n', x1(k1), xpnt(k1))
You can create formatted strings for each value of x by replacing the fprintf with sprintf and making the appropriate changes.
Walter Roberson
Walter Roberson on 14 Aug 2012
Are you sure your f90 compiler is standards compliant? The inputs do not need to be normalized for real f90.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 13 Aug 2012
The normalization rule applies only to how output values are created, and does not apply to how input values must be presented.
Look at the standard at and search down to "E Editing" and from there look at the first example, which includes
CHARACTER L*40/'1234567E2 1234.67E-3 12.4567 '/
as a string that will be validly interpreted by an E format specifier. Notice the second number there includes an E exponent but is not normalized.


Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!