Message Boards Message Boards


Mathematica Novice trying to export sensible looking table

Posted 8 years ago
1 Reply
0 Total Likes


I am trying to create a table which when exported in a .csv format and opened in MS.Excel has the following form;

i1    j1    f[i1,j1]
i1    j2    f[i1,j2]
i1    j3    f[i1,j3]
i2    j1    f[i2,j1]
i2    j2    f[i2,j2]
i2    j3    f[i2,j3]
i3    j1    f[i3,j1]
i3    j2    f[i3,j2]
i3    j3    f[i3,j3]


I have tried;

myTable = Table[{i, j, f[i, j]}, {i, 1, 3}, {j, 1, 3}]
Export["out.csv", myTable]

but it comes out as;

{{{1, 1, f[1, 1]}, {1, 2, f[1, 2]}, {1, 3, f[1, 3]}}, {{2, 1, 
   f[2, 1]}, {2, 2, f[2, 2]}, {2, 3, f[2, 3]}}, {{3, 1, f[3, 1]}, {3, 
   2, f[3, 2]}, {3, 3, f[3, 3]}}}

Which in MS.Excel looks like;

{i1    j1    f[i1,j1]}    {i1    j2    f[i1,j2]}    {i1    j3    f[i1,j3]}
{i2    j1    f[i2,j1]}    {i2    j2    f[i2,j2]}    {i2    j3    f[i2,j3]}
{i3    j1    f[i3,j1]}    {i3    j2    f[i3,j2]}    {i3    j3    f[i3,j3]}

The 3 sets of nested brackets means it doesn't come out how I wanted it.

I am sure this is a really simple/dumb question but I am a bit lost here Alex

POSTED BY: Alex Cook
Posted 8 years ago

Two issues:

1) You can use Flatten to reduce the depth of the list structure;

2) Your desired output contains commas, so you might consider outputting an Excel format or a TSV.

In[1]:= myTable = 
  Flatten[Table[{i, j, f[i, j]}, {i, 1, 3}, {j, 1, 3}], 1];

In[2]:= Export["c:\\temp\\myTable.xlsx", myTable]

Out[2]= "c:\\temp\\myTable.xlsx"

If you really need a CSV, you could convert the list entries to Strings before the export.

In[4]:= myTable2 = Map[ToString, myTable, {2}];

In[5]:= Export["c:\\temp\\myTable2.csv", myTable2]

Out[5]= "c:\\temp\\myTable2.csv"
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract