2
$\begingroup$

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\\"];
SetDirectory[direin];
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]]]

(*Winkel*)
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.

$\endgroup$
  • $\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
7
$\begingroup$

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]
Close[outputfile]

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,

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

generates this file

enter image description here

$\endgroup$
  • $\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.