mathematica transpose does not work when using NumberForm and OutputForm

the code is like

nwwa = List["#w#"]; 
nkka = List["#ks#"]; 
For[j = -4, j <= 4, j++,
     w = 16*(0.5 + 0.1*j);
     nwwa = Append[nwwa, w];
     //calculate ks
     nkka = Append[nkka, ks];
 ]
 Export["~/Documents/GB-precipitate-PRB/stability_limit/mathematica1.dat",Transpose[{OutputForm[NumberForm[nwwa, {3, 1}]],OutputForm[NumberForm[nkka, 6]]}], "Table"];

But I got the error:

“The first two levels of {{”#w#“, 1.6, 3.2, 4.8, 6.4, 8., 9.6, 11.2, 12.8, 14.4}, {”#ks#“, 0.28995, 0.1955, 0.14515, 0.1159, 0.09655, 0.0828, 0.07255, 0.0646, 0.05815}} cannot be transposed”.

the numbers are the values of nwwa and nkka.

If I just use

Export["mathematica1.dat",Transpose[{nwwa,nkka}], "Table"];

it works. but in the output file the values are like

3.200000000000003 0.19549999999999998

not what I want

3.2    0.1955

any tips?
thanks.

First, something’s missing from you code: you append ks to the current list nkka, but you never define what ks is is. (And the mysterious “//calculate ks” is not Mathematica code (the // itself has meaning in Mathematica that has nothing to do with a comment).

Second, to obtain the list nwwa, there’s no need to use an explicit loop. The following single line does it:

 nwwa = Prepend[16 (0.5 + 0.1 Range[-4, 4]), "#w#"]

But you want to use NumberForm, too. So do the following:

nwwa = 16 (0.0005 + 0.1 Range[-4, 4];
nwwa = NumberForm[nwwa, {3, 1}];
nwwa = Prepend[nwwa, "#w#"]

At least for nwwa, your export to a table should now be OK.

// calculate ks

just means some commands to calculate ks.
I want to put the data to be used by other tool to plot.
so just it is not useful that just nwwa works.

thank you.

Hi Arthur,

From the code, you are coming to Mathematica from C. I use both, and 99.9% Mathematica. In the code below, I convert to Mathematica syntax. (* This is a comment *) and * is not needed to invoke multiplication, a space is fine, but not needed after a leading number. You use For, but most often Table is better, at least more concise. NumberForm is a wrapper used for output, it should be used after all calculation, but before printing. There is a lot of information to be found in The Virtual Book. It can be found by searching the help system, or at this link: Virtual Book

Here is code as an example. (I made up a function for ks.) I also attach as a notebook, so it can be executed without deleting all the Out cells.

In[1]:= nwwa = {};

In[2]:= nkka = {};

In[3]:= For[j = -4, j <= 4, j++,
  (* calculate w *)
  w = 16 (0.5 + 0.1 j);
  (* append it *)
  nwwa = Append[nwwa, w];
  (* calculate ks *)
  ks = Sqrt[2] w;
  (* append it *)
   nkka = Append[nkka, ks];
  ];

In[4]:= nwwa

Out[4]= {1.6, 3.2, 4.8, 6.4, 8., 9.6, 11.2, 12.8, 14.4}

In[5]:= nkka

Out[5]= {2.26274, 4.52548, 6.78823, 9.05097, 11.3137, 13.5765, \
15.8392, 18.1019, 20.3647}

In[6]:= result = Transpose[{nwwa, nkka}]

Out[6]= {{1.6, 2.26274}, {3.2, 4.52548}, {4.8, 6.78823}, {6.4, 
  9.05097}, {8., 11.3137}, {9.6, 13.5765}, {11.2, 15.8392}, {12.8, 
  18.1019}, {14.4, 20.3647}}

In[7]:= formattedResult = 
  result /. {x_, y_} -> {NumberForm[x, {3, 1}], NumberForm[y, 6]};

In[8]:= Export["c:\\temp\\mathematica1.dat", formattedResult, "Table"];

In[9]:= check = Import["c:\\temp\\mathematica1.dat"]

Out[9]= {{1.6, 2.26274}, {3.2, 4.52548}, {4.8, 6.78823}, {6.4, 
  9.05097}, {8., 11.3137}, {9.6, 13.5765}, {11.2, 15.8392}, {12.8, 
  18.1019}, {14.4, 20.3647}}

In[10]:= ListPlot[check, PlotLabel -> "Mathematica can plot this!", 
 Frame -> True, FrameLabel -> {"w", "ks"}]

It works. thank you very much David.