0
$\begingroup$

I have a very long program I'm trying to stuff express as a single function. (I'll include it at the bottom of my post.) My long-term goal is to trim out the file handling portion of this function, and have another workbook call this function, passing in the table it needs, having the user manipulate the data and then press a continue button, then having it return two or three things to the original notebook.

However, when I use

functionname[x_]:=(

)

to try to encapsulate the program into an object, it generates 7-8 errors.

Any tips on how to functionize this program? (Sample data here: http://www.pastebin.ca/3061136)

Needs["Splines`"]
Needs["ErrorBarPlots`"]
SetOptions[InputNotebook[], PrintPrecision -> 10]
filelocation = SystemDialogInput["FileOpen"];
rawfiledata = Import[filelocation, "Table"];
trimmedfiledata = Drop[rawfiledata, {1}, {2, 4}];
Manipulate[moveamt = movelength, {movelength, 1, 25, 1}]
DynamicWrapper["Select the left and right edges of the data", 
 mmdata = MovingAverage[trimmedfiledata, moveamt];]
minx = Min[mmdata[[All, 1]]];
minx = minx - 0.02;
maxx = Max[mmdata[[All, 1]]];
maxx = maxx + 0.02;
miny = Min[mmdata[[All, 2]]];
maxy = Max[mmdata[[All, 2]]];
Manipulate[leftEdge = left, {left, minx, maxx, 0.001}]
Manipulate[rightEdge = right, {{right, maxx}, minx, maxx, 0.001}]
Manipulate[c1 = peak, {peak, minx, maxx, 0.001}]
Manipulate[yoff = floor, {floor, miny, maxy, 0.001}]

(*d1 is edges*)
Dynamic[d1 = Select[mmdata, leftEdge < #[[1]] < rightEdge &];]

Dynamic[Show[
  ListPlot[mmdata, PlotStyle -> Orange, 
   GridLines -> {{{c1, Orange}, leftEdge, rightEdge}, {{yoff, Red}}}],
  ParametricPlot[bsplinedat = BSplineFunction[mmdata][x], {x, -8, 8}],
  ListPlot[d1, PlotStyle -> Purple], ImageSize -> Large]]

Dynamic[f1 = 
  NonlinearModelFit[d1, a Exp[-(b (x - c1))^2] + yoff, {a, b}, x]]
Dynamic[Show[{ListPlot[d1], 
   Plot[f1[x], {x, d1[[1, 1]], d1[[-1, 1]]}, PlotRange -> All]}]]

Dynamic[Show[{ListPlot[mmdata], 
   Plot[f1[x], {x, minx, maxx}, PlotRange -> All, PlotStyle -> Red]}]]

Dynamic[smooth2 = {#[[1]], #[[2]] - f1[#[[1]]]} & /@ mmdata;]
Dynamic[ListPlot[smooth2]]
$\endgroup$
5
  • 2
    $\begingroup$ Put it inside a Module? Add semicolons to the end of each of your statements? Edit in a tiny example data set at the bottom of your post so someone else can try to run your code and see your errors and see if they fixed them? $\endgroup$
    – Bill
    Commented Jul 20, 2015 at 22:52
  • $\begingroup$ @Bill Of course, here's some sample data: pastebin.ca/3061136 $\endgroup$
    – RNPF
    Commented Jul 20, 2015 at 22:58
  • $\begingroup$ May I suggest changing the question title? There are a lot of witty users around that may want to answer your question $\endgroup$ Commented Jul 20, 2015 at 23:00
  • $\begingroup$ you might try replacing the output statements with Print[...]. i think the top-level behavior of newline-separated statements (as input into a notebook) is different when they're in a "formal" expression. $\endgroup$
    – amr
    Commented Jul 20, 2015 at 23:44
  • $\begingroup$ Why five separate Manipulate-s when one would be sufficient? $\endgroup$ Commented Jul 21, 2015 at 9:16

1 Answer 1

2
$\begingroup$

This eliminates the errors and displays some information. I do not know if this is everything you are looking for.

functionname[x_] := Module[{},
   Needs["Splines`"];
   Needs["ErrorBarPlots`"];
   SetOptions[InputNotebook[], PrintPrecision -> 10];
   filelocation = SystemDialogInput["FileOpen"];
   rawfiledata = Import[filelocation, "Table"];
   trimmedfiledata = Drop[rawfiledata, {1}, {2, 4}];
   Print[Manipulate[moveamt = movelength, {movelength, 1, 25, 1}]];
   Print[DynamicWrapper["Select the left and right edges of the data",
      mmdata = MovingAverage[trimmedfiledata, moveamt]]];
   minx = Min[mmdata[[All, 1]]];
   minx = minx - 0.02;
   maxx = Max[mmdata[[All, 1]]];
   maxx = maxx + 0.02;
   miny = Min[mmdata[[All, 2]]];
   maxy = Max[mmdata[[All, 2]]];
   Print[Manipulate[leftEdge = left, {left, minx, maxx, 0.001}]];
   Print[Manipulate[rightEdge = right, {{right, maxx}, minx, maxx, 0.001}]];
   Print[Manipulate[c1 = peak, {peak, minx, maxx, 0.001}]];
   Print[Manipulate[yoff = floor, {floor, miny, maxy, 0.001}]];
   (*d1 is edges*)
   Print[Dynamic[d1 = Select[mmdata, leftEdge < #[[1]] < rightEdge &]]];
   Print[Dynamic[Show[ListPlot[mmdata, PlotStyle -> Orange, 
      GridLines -> {{{c1, Orange}, leftEdge, rightEdge}, {{yoff, Red}}}], 
      ParametricPlot[bsplinedat = BSplineFunction[mmdata][x], {x, -8, 8}], 
      ListPlot[d1, PlotStyle -> Purple], ImageSize -> Large]]]; 
   Print[Dynamic[f1 = NonlinearModelFit[d1, a Exp[-(b (x - c1))^2] + yoff, {a, b},x]]];
   Print[Dynamic[Show[{ListPlot[d1], 
      Plot[f1[x], {x, d1[[1, 1]], d1[[-1, 1]]}, PlotRange -> All]}]]]; 
   Print[Dynamic[Show[{ListPlot[mmdata], 
      Plot[f1[x], {x, minx, maxx}, PlotRange -> All, PlotStyle -> Red]}]]]; 
   Print[Dynamic[smooth2 = {#[[1]], #[[2]] - f1[#[[1]]]} & /@ mmdata]];
   Print[Dynamic[ListPlot[smooth2]]]
];
functionname[x]
$\endgroup$
1
  • $\begingroup$ That works perfectly- Thanks! $\endgroup$
    – RNPF
    Commented Jul 21, 2015 at 16:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.