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
.
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$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$f
for two completely different things is making a mess here. $\endgroup$f
for my function solely. Thank you for pointing that out $\endgroup$