1
$\begingroup$

I am interested in implemented the following workflow. I have a Mathematica script that receives an input (manually), executes the algorithm based on that input, and stores output into a CSV file. Then I manually change input, re-run my script (with clear cache) and store my results in the same CSV file as previously, but into a new line. My question is this:

Let’s say that I have an excel or CSV file, that has all input data I need to evaluate, each into a different line. Is there any way to:

  1. Open my input list (file).
  2. Go to line 1
  3. Use this input in my script
  4. Execute script
  5. Store output in CSV file line 1.
  6. (restart process) Open input list
  7. Go to line 2
  8. Use this input in muy script
  9. Execute script
  10. Store output in CSV file line 2.
  11. Etc

N-step. Once input list has ended terminate loop

To give you a better illustration of what I am trying to achieve, here is a dummy script that reflects the complexity of what I need to achieve, along with my input list (CSV file). My goal is to be able to run eventually a much larger script, for approximately 1800 times. I managed to find how to store my results automatically with the help of the community, but I cannot figure out how/if to overcome the input process.

ClearAll["Global`*"]

file=OpenAppend["output.csv"];

x1=3;
x2=1.1;
f[x_]:=PDF[PoissonDistribution[2.5],x];
g[y_]:=PDF[ExponentialDistribution[1.2],y]
data={{x1,x2,f[x1],g[x2],f[x1]*g[x2]}};

Export[file,data,"CSV"];
WriteString[file,"\n"];

Close[file];

While here you can find my input CSV file. In this file there are several lines, each of them has 2 numbers, that refer to x1 and x2 in my given script.

My idea, but I am still trying to figoure out how to implement it is:

  • To set a counter for each evaluation starting from 1 (outside my clear all statement which is crucial in my original script).

  • Import my input csv file, feed it into a table.

  • Ask x1 and x2 to receive values from line dictated by my counter (step 1) and columns 1 and 2 respectively.

  • execute script.

  • store output.csv.

  • automatically rerun the whole script.

$\endgroup$
  • 2
    $\begingroup$ Don't write scripts. It's not the usual way to use Mathematica. Encapsulate the algorithm into a function, and call it with different parameters are many times as necessary. Also, don't do so many file manipulations unless you have a really huge input file that cannot be read into memory. Instead: import all the data, Map your processing function to it, then export it in a single go. Useful functions: Map, Table, MapIndexes, MapThread, etc. $\endgroup$ – Szabolcs Dec 7 '16 at 17:20
  • $\begingroup$ @Szabolcs You are right. My input is not complicated, only 5 variables. My only difficulty is in automating the procedure, so that I wont have to run it 1800 times, while each time I have to manually feed input. What I have done so far, is set a counter on the top of my algorithm, then clear cache apart from counter, and give counter = counter + 1 before running it. At the same time, I import my input.csv, store it into a table, and feed my variables with corresponding line equal to counter. Doing that I have managed to automate my workflow into "1800 enters" for evaluating it. $\endgroup$ – Tom Zinger Dec 7 '16 at 22:57

Your Answer

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

Browse other questions tagged or ask your own question.