# Get two columns of numerical data as output data format?

**URL:** <https://community.wolfram.com/t/get-two-columns-of-numerical-data-as-output-data-format/13783>\
**Category:** Questions\
**Tags:** equation-solving, import-and-export\
**Created:** [March 14, 2019, 8:31am UTC](https://community.wolfram.com/t/get-two-columns-of-numerical-data-as-output-data-format/13783 "2019-03-14T08:31:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![levdorosinskiy](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@levdorosinskiy](https://community.wolfram.com/u/levdorosinskiy)\
**Post date:** [March 14, 2019, 8:31am UTC](https://community.wolfram.com/t/get-two-columns-of-numerical-data-as-output-data-format/13783/1 "2019-03-14T08:31:22Z")

</div>

Hi

I am solving a system of two equations with two variables z and x repeatedly in a cycle. At each step the solution is output into a file using **Write** function. I’ve pasted a part of the file below. Apart from the numerical data, it contains also other things, like curly brackets, symbols like **z-\>**. But I need to make a file that contains only two columns of numerical data, nothing else. How can I do that?

```
{z -> -2.1337292689248377, x -> -1.0028011752012573}

{z -> -1.5494511566425633, x -> -1.0015118850013995}

{z -> 0.14651664469911463, x -> -622.9327284292021}

{z -> 2.3907571924477877, x -> -1.0037755798274968}

{z -> 3.928880270839741, x -> -52.74880957186027}

{z -> 5.2048915646949965, x -> -1.017772661581148}

{z -> 9.062107349506263, x -> -1.0515309552374674}

{z -> 10.152134293099394, x -> -1.0641971180448309}

{z -> 14.312077199555635, x -> -395.73358957571725}

```

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [March 14, 2019, 9:47am UTC](https://community.wolfram.com/t/get-two-columns-of-numerical-data-as-output-data-format/13783/2 "2019-03-14T09:47:54Z")

</div>

You get rid of the variable names with the `{x,z}/.` syntax. Then you can export to `"Table"` format:

```
sols = {x, z} /. NSolve[{x^2 + z^2 == 1, x == z}, {x, z}]
Export["~/Desktop/file.txt",
 sols,
 "Table"]

```

---

<div class="post-metadata">

**Author:** ![dimapanna](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@dimapanna](https://community.wolfram.com/u/dimapanna)\
**Post date:** [March 14, 2019, 9:55am UTC](https://community.wolfram.com/t/get-two-columns-of-numerical-data-as-output-data-format/13783/3 "2019-03-14T09:55:23Z")

</div>

Hi Lev,

A possible solution is to export your data into CSV file:  
(NOTE: this is the way to do it on-fly in the loop, if you can wait till the loop terminates,then just use Export[“MyFile.csv”, Sol] )

```
(*Define a File Path and a File Name and Open it for writing, appending \
a new data to the end of the file *)
MyPath = "C:\MY_PATH\\Wolfram \\Mathematica\\";
MyFile = "MyFile.csv";
FileH = OpenAppend[(MyPath ~~ MyFile)];

(* Solving Some Equation 10 Times in a Loop*)
Do[
  (* Solve a system of dummy equations.
       {x,z}/. is used to remove the rule sign "->" from the output*)
  Sol = {x, z} /.
    NSolve[{x^2 + z^2 == RandomReal[1], 
      z == x^2 + 0.001 RandomReal[1]}, {x, z} \[Element] Reals];
  (*Write CSV formated String into the File *)
  WriteString[FileH, ExportString[Sol, "CSV", Alignment -> Left]]
  , {10}];

(* Close the file*)
Close[FileH];

```

The resulted file output looks like this:

.

 ![9685Capture.PNG](https://global.discourse-cdn.com/wolfram/original/3X/a/7/a795ac254f0aaf6fc844e9e7677633901d775e51.png)

---

<div class="post-metadata">

**Author:** ![levdorosinskiy](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@levdorosinskiy](https://community.wolfram.com/u/levdorosinskiy)\
**Post date:** [March 14, 2019, 12:03pm UTC](https://community.wolfram.com/t/get-two-columns-of-numerical-data-as-output-data-format/13783/4 "2019-03-14T12:03:14Z")

</div>

Thank you. My problem is somewhat different and I could not figure out how to apply your solutions to my case

```
sol[p1_?NumberQ, p2_?NumberQ, p3_?NumberQ, p4_?NumberQ, p5_?NumberQ] := 
  FindRoot[my equations, {{z, p5}, {x, 5}}];

```

And then in the cycle:

```
Write[file, sol[p1, p2, p3, p4, p5]];

```

My equations are non-polynomial. Besides, they have 5 parameters p1 - p5 which are varied et each step of the cycle. That’s why I use FIndRoot instead of NSolve and I define function sol[…] with parameters.

In this case there is no expression like sol[…] = something. That’s why I could not figure out where to insert `{x, z} /.`

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [March 14, 2019, 12:48pm UTC](https://community.wolfram.com/t/get-two-columns-of-numerical-data-as-output-data-format/13783/5 "2019-03-14T12:48:14Z")

</div>

Try something like this:

```
sol[p1_?NumberQ, p2_?NumberQ, p3_?NumberQ, p4_?NumberQ, 
   p5_?NumberQ] := {z, x} /. FindRoot[my equations, {{z, p5}, {x, 5}}];
Export[file, sol[p1, p2, p3, p4, p5], "Table"]

```

Instead of writing the numbers separately into the file, you can do it at the end.

---

<div class="post-metadata">

**Author:** ![dimapanna](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@dimapanna](https://community.wolfram.com/u/dimapanna)\
**Post date:** [March 14, 2019, 12:50pm UTC](https://community.wolfram.com/t/get-two-columns-of-numerical-data-as-output-data-format/13783/6 "2019-03-14T12:50:09Z")

</div>

Hi Lev,  
The fact that you are using FindRoot instead of NSolve doesn’t really matter, please see the following code:

```
(*Define File Path and File Name and Open it for writing appending \
new data to the end of the file *)
MyPath = "C:\\MyPath\\Wolfram \
Mathematica\\";
MyFile = "MyFile.csv";
FileH = OpenAppend[(MyPath ~~ MyFile)];

sol[p1_, p2_, p3_, p4_, p5_] := {x, z} /. 
  FindRoot[{Exp[p1 x] == p2 x, Exp[p3 z] == p4 z}, {{z, p5}, {x, p5}}]

(* Solving Some Equation in the Loop*)
Do[
  (*{x,z}/. is used to remove rule sign "\[Rule]" *)
  Sol = {sol[1, 5, 1, 4, 0]};
  (*Write CSV formated String into the File *)
  WriteString[FileH, ExportString[Sol, "CSV", Alignment -> Left]]
  , {10}];

(* Close the file*)
Close[FileH];

```

The output is :

 ![7386Capture.PNG](https://global.discourse-cdn.com/wolfram/original/3X/5/b/5bdb529a7a07069f3a7ef990bb5b0547f8aae278.png)

Please notice, that this is only needed when you HAVE TO update the file on-fly in the loop, as Gianluca Gorni mentioned you can do it much easier outside the loop using Export[] with Table attribute
