3
$\begingroup$

I have a problem that really limits my productivity. I need to run a script several times, export results in an .xls file. Instead of manually fullfilling my results in my .xls file, I thought about exporting my results in an excel file and just copy them. I know how to export my results each time in an excel file (this will overwrite my previous file each time, so I will have to copy results before rerunning script), I know how to automatically change the name of my exported file (receiving a unique ID so that I can then copy all of my exported files into my desired one), but the optimal solution would be to run my script, store results in an .xls file, and then rerunning it, having new values stored in previous files new line etc.

I tried many solutions found scatted in the web but nothing works, my exported file always remains in tacked, or gets corrupted (I tried PutAppend, Openwrite and other solutions, none of them worked , i guess I did something wrong).

Here is a dummy script:

x1=1;
f[x_] := PDF[PoissonDistribution[2.5], x];
myExportLine = {{"Text",x1, f[x1]}};

Export["Test_file.xls", myExportLine, "XLS"]

I need to provide values for my script manually (meaning I do not need to run any loop for my x1 values. Any idea how to implement it?

Example of fail attempt:

f1=OpenAppend["Test_file.xls"]

x2=2;
f[x_]:=PDF[PoissonDistribution[2.5],x];
myExportLine2={{"Text",x2,f[x2]}};
WriteString[f, myExportLine2, "\n"];
Close[f1];

edit: I corrected the repentance of f in two different variables. Renamed one of them into f1.

$\endgroup$
  • 1
    $\begingroup$ WriteString writes that string literally and doesn't know anything about formats. An Excel file has a complicated internal structure. If you just append some arbitrary bytes at the end, you will corrupt the file. $\endgroup$ – Szabolcs Dec 6 '16 at 14:03
  • $\begingroup$ How about storing the whole list im memory (in Mathematica) and saving it just once? myExport = Table[{StringTemplate["Test: ``"][x1], x1, f[x1]}, {x1, 0, 2, .1}]; Export["Test_file.xls", myExport, "XLS"], or do you want to close Mathematica in between? $\endgroup$ – grbl Dec 6 '16 at 14:04
  • $\begingroup$ Szabolcs thank you for clarifying that to me. grbl this method keeps overwriting on my previous results. I do not need to close mathematica, but I need to quit kernet at each run. $\endgroup$ – Tom Zinger Dec 6 '16 at 16:25
  • 1
    $\begingroup$ aside to your question, using the symbol f for two completely different things is making a mess here. $\endgroup$ – george2079 Dec 6 '16 at 16:34
  • $\begingroup$ @george2079 yes you are right, I misstyped while providing my example. Originally i used f for my function solely. Thank you for pointing that out $\endgroup$ – Tom Zinger Dec 7 '16 at 14:13
3
$\begingroup$

It is not possible to append to an Excel file. The only way is to read the data into memory, Append the additional data in-memory, the re-export the whole thing to disk. This is of course inefficient and it is up to you to decide if it is suitable for your use case.

I believe that the structure of Excel files is such that it isn't possible to append with software other than Mathematica either.

Alternatively, don't use Excel. Use a simple and predictable format like CSV:

$\endgroup$
  • $\begingroup$ This seems to do the trick. CSV is also convienient way to get where I want to go. I was trying the very same method as question attached, but I didn't know that xls files where not compatible. Thanks for sharing that. $\endgroup$ – Tom Zinger Dec 6 '16 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.