How can I align pos/neg numbers with fprintf?

29 views (last 30 days)
I'm trying to recreate the a specific file format (K file), that looks as follows
219 8.0199807e-001 -2.2447408e-002 4.5720000e-002
220 8.0199807e-001 -2.2447408e-002 5.0800000e-002
221 8.0199807e-001 2.3252592e-002 2.2204460e-016
My code now can assemble the matrix correctly, but I'm having trouble aligning positive value numbers with negative ones. For instance, the code above shows up as:
219 1.8290000e+00 -2.7420000e-02 1.0160000e-03
220 1.8290000e+00 -2.7420000e-02 5.0800000e-03
221 1.8290000e+00 9.1400002e-03 -3.5560000e-02
The value in the 3rd row and column is left adjusted which is a huge hassle later on. I can't simply add a "+".
What can I do to properly align them?
The format code I have now for the fprintf command to write them is:
formatspec = ('\t %4i %9.7e %9.7e %9.7e \n');
Any help is greatly appreciated!

Accepted Answer

Star Strider
Star Strider on 5 Jan 2015
Edited: Star Strider on 5 Jan 2015
Probably the easiest way is to precede each width specifier with a space.
Example:
x = [pi; -pi]
s1 = sprintf('% .4f\n', x)
Another option is to force the sign to be printed:
x = [pi; -pi]
s2 = sprintf('%+.4f\n', x)
producing respectively:
s1 =
3.1416
-3.1416
s2 =
+3.1416
-3.1416
Note that ‘sprintf’ and ‘fprintf’ are essentially the same, except for the nature of the output. I chose ‘sprintf’ to illustrate the concept.

More Answers (2)

Image Analyst
Image Analyst on 5 Jan 2015
9.7 is not big enough to hold the variable so it automatically expands the field, and because one has - and one doesn't, it expands them by different amounts. Try %15.7e or something like that.

dpb
dpb on 5 Jan 2015
>> xx=[219 8.0199807e-001 -2.2447408e-002 4.5720000e-002
220 8.0199807e-001 -2.2447408e-002 5.0800000e-002
221 8.0199807e-001 2.3252592e-002 2.2204460e-016];
>> fmt=['\t %4i' repmat('%12.3e',1,2) '%12.3e\n'];
>> fprintf(fmt,xx.')
219 8.020e-01 -2.245e-02 4.572e-02
220 8.020e-01 -2.245e-02 5.080e-02
221 8.020e-01 2.325e-02 2.220e-16
>>
Should work. I shortened the fields but the alignment should work fine if you count spaces correctly. NB there are no extra blanks in the formatting string.
Unless the file format demands the tab I'd forego it and use a fixed width field of the proper width for the integer field, too. Tabs are dependent upon the displaying device for interpretation so can often cause problems altho only the single one at the beginning of the line is less of an issue than trying to use them embedded within a line.
Let's see, for your 7 decimal places means you need--
7
plus 4 for the exponent field
plus 1 for the decimal point
plus 1 for the sign field
plus 1 (at least) for the space between fields
That's a field width of 14 and you asked for only 9. Hence Matlab did what you asked; used the minimum possible to fill the field resulting in the shifting of position when the positive sign wasn't needed.
AFAIK, there's no standard way in C to write the three-digit exponent field; if you must have that you'll have to write it specifically.

Categories

Find more on Numeric Types 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!