Message Boards Message Boards

0
|
5033 Views
|
7 Replies
|
2 Total Likes
View groups...
Share
Share this post:
GROUPS:

Export nested table to a file

Posted 9 years ago

I want to export a nested table to just any file would do the job. So .txt or .dat or .xls ,... anything.

The table is

final = Prepend[
   Table[{j, force[[j, 3]], 
     Table[{lengthall[[i + 1]], visinaallc[[i + 1]]*100, 
       optimumfall[[j, i]], F[N[lengthall[[i + 1]]]]}, {i, 1, 
       n - 4 + m, 1}]}, {j, 1, 3, 1}], {"Cell number [N]", 
    "Cell width [cm]", "Profile position [%]", "Profile height [cm]", 
    "Optimum ballooning [%]", "Spanwise force [N]"}];

and the result to the code above is... quite long:

{{"Cell number [N]", "Cell width [cm]", "Profile position [%]", 
  "Profile height [cm]", "Optimum ballooning [%]", 
  "Spanwise force [N]"}, {1, 
  34.78, {{1., 13.1772, 5.21612, 16.7935}, {2., 18.7884, 5.7396, 
    16.7935}, {3., 22.8883, 5.73783, 16.7935}, {4., 26.129, 5.62976, 
    16.7935}, {5., 28.78, 5.58411, 16.7935}, {6., 30.9876, 5.38298, 
    16.7935}, {7., 32.8434, 5.25782, 16.7935}, {8., 34.41, 5.17249, 
    16.7935}, {9., 35.7336, 5.11541, 16.7935}, {10., 36.8492, 5.04614,
     16.7935}, {11., 37.7853, 4.9356, 16.7935}, {12., 38.5649, 
    4.78775, 16.7935}, {13., 39.2077, 4.60811, 16.7935}, {14., 
    39.7308, 4.44532, 16.7935}, {15., 40.1492, 4.30505, 
    16.7935}, {16., 40.4766, 4.18489, 16.7935}, {17., 40.7255, 
    4.08309, 16.7935}, {18., 40.9076, 3.9976, 16.7935}, {19., 41.0343,
     3.92265, 16.7935}, {20., 41.1167, 6.40053, 26.6881}, {25., 
    41.0503, 6.29634, 26.6881}, {30., 40.3538, 6.37692, 
    26.6881}, {35., 39.2796, 6.54435, 26.6881}, {40., 37.7837, 
    3.64546, 16.1072}, {45., 35.7833, 3.7278, 16.1072}, {50., 33.3253,
     3.8508, 16.1072}, {55., 30.4736, 4.01936, 16.1072}, {60., 
    27.3038, 8.56674, 1.74148}, {65., 23.898, 9.56134, 1.74148}, {70.,
     20.3405, 10.7995, 1.74148}, {75., 16.7136, 12.3246, 
    1.74148}, {80., 13.0954, 11.0194, 5.91974}, {85., 9.55685, 
    12.5594, 5.91974}, {90., 6.16075, 14.3792, 5.91974}, {95., 
    2.96053, 16.5063, 5.91974}, {100., 0., 18.9619, 5.91974}}}, {2, 
  29.72, {{1., 13.1772, 6.30227, 16.7935}, {2., 18.7884, 6.38955, 
    16.7935}, {3., 22.8883, 6.08746, 16.7935}, {4., 26.129, 5.77556, 
    16.7935}, {5., 28.78, 5.56883, 16.7935}, {6., 30.9876, 5.27947, 
    16.7935}, {7., 32.8434, 5.07954, 16.7935}, {8., 34.41, 4.93164, 
    16.7935}, {9., 35.7336, 4.82119, 16.7935}, {10., 36.8492, 4.71512,
     16.7935}, {11., 37.7853, 4.59064, 16.7935}, {12., 38.5649, 
    4.44901, 16.7935}, {13., 39.2077, 4.29239, 16.7935}, {14., 
    39.7308, 4.15376, 16.7935}, {15., 40.1492, 4.03599, 
    16.7935}, {16., 40.4766, 3.93646, 16.7935}, {17., 40.7255, 
    3.85322, 16.7935}, {18., 40.9076, 3.78424, 16.7935}, {19., 
    41.0343, 3.72485, 16.7935}, {20., 41.1167, 4.3329, 26.6881}, {25.,
     41.0503, 4.20177, 26.6881}, {30., 40.3538, 4.22564, 
    26.6881}, {35., 39.2796, 4.30944, 26.6881}, {40., 37.7837, 
    3.87174, 16.1072}, {45., 35.7833, 4.03026, 16.1072}, {50., 
    33.3253, 4.25265, 16.1072}, {55., 30.4736, 4.5486, 16.1072}, {60.,
     27.3038, 8.39489, 1.74148}, {65., 23.898, 9.60291, 
    1.74148}, {70., 20.3405, 11.15, 1.74148}, {75., 16.7136, 13.1196, 
    1.74148}, {80., 13.0954, 17.4804, 5.91974}, {85., 9.55685, 
    21.2078, 5.91974}, {90., 6.16075, 25.9435, 5.91974}, {95., 
    2.96053, 31.9446, 5.91974}, {100., 0., 39.5358, 5.91974}}}, {3, 
  25.68, {{1., 13.1772, 11.3841, 16.7935}, {2., 18.7884, 9.49492, 
    16.7935}, {3., 22.8883, 8.16463, 16.7935}, {4., 26.129, 7.24228, 
    16.7935}, {5., 28.78, 6.60826, 16.7935}, {6., 30.9876, 6.07815, 
    16.7935}, {7., 32.8434, 5.69014, 16.7935}, {8., 34.41, 5.3948, 
    16.7935}, {9., 35.7336, 5.16612, 16.7935}, {10., 36.8492, 4.97713,
     16.7935}, {11., 37.7853, 4.80999, 16.7935}, {12., 38.5649, 
    4.65932, 16.7935}, {13., 39.2077, 4.5212, 16.7935}, {14., 39.7308,
     4.4058, 16.7935}, {15., 40.1492, 4.31152, 16.7935}, {16., 
    40.4766, 4.23505, 16.7935}, {17., 40.7255, 4.17382, 
    16.7935}, {18., 40.9076, 4.1255, 16.7935}, {19., 41.0343, 4.08673,
     16.7935}, {20., 41.1167, 3.44659, 26.6881}, {25., 41.0503, 
    3.34124, 26.6881}, {30., 40.3538, 3.36315, 26.6881}, {35., 
    39.2796, 3.43499, 26.6881}, {40., 37.7837, 3.37109, 
    16.1072}, {45., 35.7833, 3.5368, 16.1072}, {50., 33.3253, 3.76837,
     16.1072}, {55., 30.4736, 4.07748, 16.1072}, {60., 27.3038, 
    6.53227, 1.74148}, {65., 23.898, 7.51349, 1.74148}, {70., 20.3405,
     8.78497, 1.74148}, {75., 16.7136, 10.4254, 1.74148}, {80., 
    13.0954, 10.9868, 5.91974}, {85., 9.55685, 13.2053, 
    5.91974}, {90., 6.16075, 15.9992, 5.91974}, {95., 2.96053, 
    19.4936, 5.91974}, {100., 0., 23.8271, 5.91974}}}}

Now final//TableForm shows exactly what I want to export to the file. But Export["filename.dat",final,"Table"] doesn't do the job. It exports the table in some strange form, where also ""{{}}"" brackets are included. I don't like that. :/ I had problems with that before and usually Flatten did the job, but this time it helps to get rid of the brackets but at the same time it get's rid of the columns too.:/ Attached I am sending you the exported file. Any ideas on what can I do with it?

Attachments:
POSTED BY: Mitja Jan?i?
7 Replies
Posted 9 years ago

Hi Mitja,

You say "just any file." If your only need is to read the data back into Mathematica, then you could use DumpSave to save the full definition to a file in a Wolfram-proprietary binary format, which can be read back into a Mathematica session with Get.

Best, David

POSTED BY: David Keith

Nono I don't want to read that file with Mathematica anymore!

This file, this huge table I called final, are my final results. I only need them in a .txt file because I have to send it to other people - who have no idea what Mathematica is. Nobody will ever work with those results in Mathematica again.

POSTED BY: Mitja Jan?i?

Ok, I have found the problem... It's if sentence... If you replace StandardForm["Opening"] with 0, everything works.

POSTED BY: Mitja Jan?i?

Hi Henrik,

I apologize to open this topic again but it suddenly stopped working for no reason at all.

Currently my code is:

final = Table[{Prepend[
     Table[{j, force[[j, 3]], chordlength[[j]], force[[j, 4]], 
       profilelength[[i + 1]], optimumDensitySplitTop[[j, i]], 
       If[BeginCell < profilelength[[i + 1]] < EndCell, 
        StandardForm["Opening"], optimumDensitySplitBtm[[j, i]]], 
       norm[[j]]*
        DensityTop[[j]][profilelength[[i]]*chordlength[[j]]/10^4], 
       If[BeginCell < profilelength[[i + 1]] < EndCell, 
        StandardForm["Opening"], 
        norm[[j]]*
         DensityBtm[[j]][profilelength[[i]]*chordlength[[j]]/10^4]], 
       Abs[ptop[[j]][profilelength[[i + 1]]*chordlength[[j]]/10^4] - 
         pressure[[1, 2]]], 
       Abs[pbtm[[j]][profilelength[[i + 1]]*chordlength[[j]]/10^4] - 
         pressure[[1, 2]]]}, {i, 1, n - 10 + m, 
       1}], {"Cell number [N]", "Cell width [cm]", "Chord [cm]", 
      "Height factor", "Profile position [%]", "Ballooning Top [%]", 
      "Ballooning Btm [%]", "Force top[N/m]", "Force btm [N/m]", 
      "Presssure diff top [Pa]", "Presssure diff btm [Pa]"}]}, {j, 1, 
    Length[force[[All, 1]]], 1}];

And applying TableForm to that works really nicely, yet exporting with

Export["C:\\Users\\Mitja\\Documents\\nova\\2015\\results_\
constantForce.txt", ToString[TableForm@final]];

Doesn't work at all. The exported file is attached.

Attachments:
POSTED BY: Mitja Jan?i?

Hi Mitja,

assuming final is your final result, try:

Export["filename.dat", ToString[TableForm@final]]

Regards -- Henrik

POSTED BY: Henrik Schachner

Hi, Your assumption is correct! And also your solution works perfectly. Attached I have the file as I wanted it. Just one more question, is it possible to align the columns with their headings?

EDIT: One more question that might not be related to this topic. N[Pi,3] works perfectly, but if I use an element from any table for example N[table[[5]],3] doesn't work at all. Is there a reason why and a solution to this?

Attachments:
POSTED BY: Mitja Jan?i?

Well, one has to play around a little bit, using the documentation about TableForm, ...

Anyway, here it is:

headings = final[[1]];
content = final[[2 ;;]];
Export["filename.dat", 
  ToString @ TableForm[{#1, #2, Sequence @@ Transpose[#3]} & @@@ content, TableHeadings -> {None, headings}]];

N[table[[5]],3] doesn't work at all.

It depends what table represents. E.g.:

table = Range[10];
N[table[[5]], 3]

works as expected.

Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract