Let's say I have some Table object. The elements of this table are accessed as Table[[i,j,k]].

For each element in the array I want to convert it to FortranForm and then construct a string of the form STRING_IJK = value where value is the specific table element FortranForm[Table[[i,j,k]]].

I then want to save all of these strings to an external file.

Is there a method to do this? Thanks


1 Answer 1


Assuming your table is called something like myTable:

fortranTable = Flatten[Table[
  "STRING_" <> ToString[i] <> ToString[j] <> ToString[k] <> " = " <> 
  ToString[myTable[[i, j, k]] // FortranForm], 
  {i, Dimensions[myTable][[1]]}, 
  {j, Dimensions[myTable][[2]]},
  {k, Dimensions[myTable][[3]]}]]

<> is the concatenation operator for strings, so I'm just converting the indices to strings and then joining them together. I have an expression like x^26 in position 111, I get: STRING_111 = x**26.


If you want to export the final table, the following works but tweaks can be made depending on exactly what you want the output to look like.

Export["filelocation/filename.txt", fortranTable]

I believe you can use either double backslashes \\ to separate the parts of file names or a single slash, and of course filelocation and filename should be changed to suit your needs, but the quotation marks are required. This should give you a file where each entry in the table has its own line (that is, they're separated by '\n').

  • $\begingroup$ This is great. Is there a way to then save the output to file? $\endgroup$ Mar 8, 2019 at 10:11
  • $\begingroup$ @user1887919 I've edited my answer to include the export as well. $\endgroup$
    – MassDefect
    Mar 8, 2019 at 16:17

Your Answer

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

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