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.
Answer
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:
Comments
Post a Comment