In Fortran, we can easily get the stardard and easy output format. Now, we only focus on the Integer output. See here How to realize this kind of output as formatted text file using Mathematica ? (Then it can be read in Fortran easily).Take a simple example, I have a list,

   list=Table[(i + j) i (i - j), {i, 1, 2}, {j, 1, 22}];

I want to get the output data in a text file and

  1. Every integer should be printed with 6 positions. (It is I6 in Fortran).

  2. The maximum line length in the text file should be limited to 120.

    enter image description here

  • $\begingroup$ Your question is not clear. Is your goal to format output in a Mathematica notebook, or is it exporting your data to disk as formatted text? $\endgroup$
    – m_goldberg
    Mar 24, 2015 at 3:08
  • $\begingroup$ To obtain the formatted text file. $\endgroup$
    – Orders
    Mar 24, 2015 at 3:10
  • $\begingroup$ OK, but in a file or on the screen in a notebook window? $\endgroup$
    – m_goldberg
    Mar 24, 2015 at 3:12
  • $\begingroup$ The output should be stored in a text file. $\endgroup$
    – Orders
    Mar 24, 2015 at 3:22
  • $\begingroup$ Is the maximum line length something you are supplying and is represented in the actual file, e.g. -399\n -440, or were you just saying that's how the file would look in an editor whose maximum line length is set to 120. $\endgroup$ Mar 24, 2015 at 3:24

1 Answer 1

data = Table[(i + j) i (i - j), {i, 1, 2}, {j, 1, 22}]; 

The first step is to define a function to break up each row in the data into lines not exceeded the specified maximum line length.

prep[row_, fieldWidth_, lineLength_] :=
  Module[{items = Floor[lineLength/fieldWidth]}, 
    Partition[row, items, items, 1, {}]]

The second step is to do the breaking. I have shorten the lines to 80 characters for better looking output to show here.

lines = Flatten[prep[#, 6, 80] & /@ data, 1];

The third step is to make the strings

output = 
  StringJoin @@@ 
    Map[StringTake[StringJoin["     ", ToString@#], -6] &, lines, {2}]

The final step is to export the strings. I use ExportString for demonstration.

ExportString[output, "Text"]


You can use something similar to

Export["~/Desktop/test.txt", output, "Text"]

to put the strings into a disk file.


As suggested by SjoerdC.deVries in a comment,

output = 
  StringJoin @@@ 
    Map[ToString[NumberForm[#, 6, NumberPadding -> " "]] &, lines, {2}];

is an alternative way to make the strings.

  • $\begingroup$ How about IntegerString? $\endgroup$ Mar 24, 2015 at 6:32
  • $\begingroup$ @SjoerdC.deVries. Not sure what you are getting at, but consider that IntegerString[-42, 10, 6] gives "000042". To munge that into the form I actually need doesn't seem better than what I did. $\endgroup$
    – m_goldberg
    Mar 24, 2015 at 14:13
  • $\begingroup$ OK, that was too quick. How about NumberForm[-42, 6, NumberPadding -> " "] // ToString then? $\endgroup$ Mar 24, 2015 at 14:17
  • $\begingroup$ @SjoerdC.deVries. That'a good idea. I added an update giving it as an alternative. $\endgroup$
    – m_goldberg
    Mar 25, 2015 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.