I wrote a program (with help of the stackexchange) that is supposed to run in a loop. Each time the program runs, it exports a small data. Is it possible to generate a table where each run adds the new data?

I am still working at the loop but here is the programm for so far:

direin = SetDirectory[StringJoin[NotebookDirectory[], "Eingang\\"]];
diraus = StringJoin[NotebookDirectory[], "Ausgang\\"];
files = FileNames[{"*.PNG"}, FileNameJoin[{Directory[]}]];
img = Import /@ FileNames[files[[1]]];
img = img[[1]];
noBorder = ImagePad[img, -BorderDimensions[img]];
{w, h} = ImageDimensions[noBorder];
wnd = Outer[Times, Array[HammingWindow, h, {-.5, .5}], 
   Array[HammingWindow, w, {-.5, .5}]];

rawPixels = ImageData[noBorder][[All, All, 1]];
imgTimesWnd = (rawPixels - Mean[Flatten[rawPixels]])*wnd;

ft = Fourier[imgTimesWnd];
center = Floor[Dimensions[ft]/2];
ft = RotateRight[ft, center];

Image[Rescale[Log[Abs[ft] + 10^-3]]]

brightestOffset = First[Position[Abs[ft], Max[Abs[ft]]]] - center
maxAngle = ArcTan @@ N[brightestOffset/{h, w}]
inDeg = 180 / \[Pi]; 
getDeg = inDeg*maxAngle
namebild = FileBaseName[files[[1]]]
Export[namebild <> "_Winkel.txt", getDeg];

Please note that this program is supposed to export a specific angle.

  • $\begingroup$ It isn't really possible to run your code, since I don't have your directory structure and image files, but you essentially create a text file when you run that contains a single angle in degrees? And you want to be able to run the program many times and write to that same file? $\endgroup$ – Jason B. Oct 28 '15 at 8:34

Rather than Export a single number to a file, open the file as a stream and write to it,

ang = RandomReal[2 π] /Degree;
outputfile = OpenAppend["file.txt"]
Write[outputfile, ang]

If you run OpenAppend on a file that doesn't exist, it will create it. Then when you use Write, it writes the content to the file, followed by a newline. When you open the file again using OpenAppend, it will simply write the text on the next line and end in a new line.

Running this loop,

 ang = RandomReal[2 \[Pi]] /Degree;
 outputfile = OpenAppend["file.txt"];
 Write[outputfile, ang];
 , {20}]

generates this file

enter image description here

  • $\begingroup$ that was exactly what i was looking for!!! thx for your help. $\endgroup$ – Felicitas Oct 28 '15 at 9:56

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.