# About writing a file using Do loop

I want to write a file using Do loop. The loop computes a list of 9 functions whose outputs are floating point numbers. One of the functions produces large numbers which are generally expressed in scientific notation.

I intend to write the file in such a way so that while importing the file I can get a proper matrix output of $$(n\times 9)$$ where $$n$$ is the number of iterations.

I wrote the following code for $$n=10$$, but the output is not as per my expectation.

SetDirectory["E:\\"];
file2 = OpenWrite["file2.dat", FormatType -> OutputForm];
Do[F[i] =
N[{Cos[i]*Sin[i], Exp[i], i^2, Cos[i]*Exp[i], i, Sin[i] + Cos[i],
Log[i], Exp[Sin[i]], i^i}];
Write[file2, F[i]]; , {i, 1, 10}]
Close[file2];


The output is

MatrixForm[Import["E:\\file2.dat"]]

(*{{0.454649,, 2.71828,, 1.,, 1.46869,, 1.,, 1.38177,, 0.,, 2.31978,, 1.}}

{{-0.378401,, 7.38906,, 4.,, -3.07493,, 2.,, 0.493151,, 0.693147,, 2.48258,, 4.}}

{{-0.139708,, 20.0855,, 9.,, -19.8845,, 3.,, -0.848872,, 1.09861,, 1.15156,, 27.}}

{{0.494679,, 54.5982,, 16.,, -35.6877,, 4.,, -1.41045,, 1.38629,, 0.469164,, 256.}}

{{-0.272011,, 148.413,, 25.,, 42.0992,, 5.,, -0.675262,, 1.60944,, 0.383305,, 3125.}}

{{-0.268286,, 403.429,, 36.,, 387.36,, 6.,, 0.680755,, 1.79176,, 0.756226,, 46656.}}

{{0.495304,, 1096.63,, 49.,, 826.754,, 7.,, 1.41089,, 1.94591,, 1.92897,, 823543.}}

{{-0.143952,, 2980.96,, 64.,, -433.729,, 8.,, 0.843858,, 2.07944,, 2.68951,}

{}

{7}

{1.67772, 10, }}

{{-0.375494,, 8103.08,, 81.,, -7382.96,, 9.,, -0.499012,, 2.19722,, 1.51001,}

{}

{8}

{3.8742, 10, }}

{10}

{{0.456473,, 22026.5,, 100.,, -18481.8,, 10.,, -1.38309,, 2.30259,, 0.58041,, 1., 10, }}*)


What I have understood is that the term $$i^i$$ (which produces large numbers, and therefore, whose outputs are expressed in scientific notation) is the main reason for such output (e.g., inclusion of extra rows). Unfortunately, I could not find a way to fix this and am eagerly waiting help.

• I recommend Export. – Αλέξανδρος Ζεγγ Oct 7 '18 at 7:51
• @Carl Woll Thanks for all the necessary corrections. – Soumyajit Roy Oct 7 '18 at 19:43

You can try the following. I writes the expression for the matrix into a .m file. For this, additional braces and commas have to be written to file, too. Writing also "DeveloperToPackedArray@" in front guarantees that the matrix is converted to a packed array when imported. Also leave away FormatType -> OutputForm opening the file.

F[i_] := N[{Cos[i]*Sin[i], Exp[i], i^2, Cos[i]*Exp[i], i, Sin[i] + Cos[i], Log[i], Exp[Sin[i]], i^i}];
n = 10;
file2 = OpenWrite["file2.m"];
WriteString[file2, "DeveloperToPackedArray@{\n"];
Do[
Write[file2, F[i]];
If[i < n, WriteString[file2, ",\n"]];,
{i, 1, n}];
WriteString[file2, "}"];
Close[file2];


This my far not the most efficient way of writing and loading a matrix from file. Try also Export with formats with the Matlab matrix formal MAT or Mathematica's file dump format MX. These formats a binary and cannot be written line-by-line (as far as I know). See the docs for further details.

• Thank you very much. This has solved my problem. – Soumyajit Roy Oct 7 '18 at 19:52
• Fine! You're welcome. – Henrik Schumacher Oct 7 '18 at 20:00

Don't use OutputForm, and do set PageWidth:

file2 = OpenWrite["file2.dat", PageWidth->Infinity];
Do[
F[i] = N @ {
Cos[i] Sin[i], Exp[i], i^2, Cos[i] Exp[i], i,
Sin[i]+Cos[i], Log[i], Exp[Sin[i]], i^i
};
Write[file2, F[i]],
{i,1,10}
]
Close[file2];


Then:

Dimensions @ Import["file2.dat"]


{10, 9}

• Even after omitting OutputForm, I could not get the desired output. – Soumyajit Roy Oct 7 '18 at 19:50
• @SoumyajitRoy I forgot I had used SetOptions on OpenWrite. Should work now. – Carl Woll Oct 7 '18 at 20:41
• I am sorry to say that this is still not the output which I am expecting, although the dimensions are {10,9}. Here is the difference with Henrik's code: i.imgur.com/X7RBPja.png – Soumyajit Roy Oct 8 '18 at 6:04