0
$\begingroup$

In my project, there are some variables that I want to define locally so that my defined function has no Manipulate dependency with each other when calling the function twice. By using Module, I am able to avoid this dependency by letting these variables be defined locally.

However, when evaluating the same function twice, it creates a new variable in the global setting due to Module. I have attempted to avoid this by defining the previous variable Globally after it is locally defined by implementing LocalizeVariables->False so that I can use this same variable every time I evaluate the same function.

Is there a happy medium where once a localized variable is defined, it can be used globally? Thank you!

EDIT: Here is a visually inaccurate (in terms of Manipulate visual placement) but working example to illustrate the issue that I am facing.

Testlistplot[{lis__}, {xmin_, xmax_, 
   sampint_, {xname_String, xunit_String}}] := ( 
  Module[{xInt, lists}, lists = {lis, {}};
   If[ValueQ[xInt] == False, xInt = {xmin, xmax};];
   Manipulate[Show[Table[ListLinePlot[lists[[k]], AxesLabel -> {xunit}, 
       DataRange -> {xmin, xmax}, PlotRange -> 
        If[ValueQ[xInt] == False, {{xmin, xmax}}, {xInt}]], {k, 1, 
       Length[lists]}]], {{xInt, {xmin, xmax}, Row[{xname}]}, xmin, 
     xmax, ControlType -> IntervalSlider, Appearance -> "Labeled", 
     Method -> "Stop", MinIntervalSize -> 0.001, 
     ImageSize -> {300, 25}}, ControlPlacement -> {Bottom}, 
    LocalizeVariables -> False]])
signal1 = Table[t^2, {t, 0, 3, 0.01}];
Testlistplot[{signal1}, {0, 3, 0.01, {"x-Axis", "seconds"}}]

Running the above code will store a Global variable defined as xInt$#### and lists$#### (#### indicates a randomly chosen number). This is perfectly fine. However, re-evaluating the above code will produce a new set of Global variables defined as xInt$&&&& and lists$&&&& (&&&& indicates another randomly chosen number different than ####). Is there some way to access xInt$#### and lists$#### after the first evaluation?

My inevitable goal is to try and distinguish "global variables" per cell. Therefore, every time when someone edits the cell, the "global variables" stored in that cell will be maintained. When someone inputs the same code in another cell, this will create a new set of "global variables" in that cell different than the ones previously defined. Thank you and sorry for the confusion!

$\endgroup$
  • $\begingroup$ I am not sure I completely understand your question. It might be that the function Unique can help you. It creates variables with a unique name during a Mathematica session, just as Module does. $\endgroup$ – Fred Simons Sep 17 '16 at 6:49
  • $\begingroup$ I have added a working minimal example of my code to illustrate my problem. Thank you and sorry for the earlier confusion. $\endgroup$ – Wilson Sep 17 '16 at 15:10
  • $\begingroup$ @Wilson For me to understand, let's say that you have in two different cells: Module[{x}, x]. At each new evaluation, independently of the cell, you will get a new x$... symbol. Do you want the evaluation in the first cell to always give the same x$..., and the same for the second cell with another x$...? $\endgroup$ – user31159 Sep 17 '16 at 16:32
  • $\begingroup$ @Wilson For your first question, you can use Echo at the first occurence of xInt and lists within the module. So something like Echo[xInt] for instance. This will print the value of xInt. You could also use a global variable to store their values, so xIntGlobal = xInt would be written in the module, say right after the declaration of local variables. I'm not sure this is what you're after though. $\endgroup$ – user31159 Sep 17 '16 at 16:43
  • $\begingroup$ @Xavier Your first comment is correct. I need this because I am trying to create a program for the engineering department and they require that some of the global variables are stored locally per cell. I will take a look at your Echo suggestion later today. $\endgroup$ – Wilson Sep 17 '16 at 16:52
2
$\begingroup$

I'm having a hard time understanding what you are looking for. Your example code demonstrates the problem with giving the option LocalizeVariables -> False to Manipulate, but doesn't really tell me what would you would accept as a replacement. The best idea I can come up with is to memo-ize the current value of {{xmin, xmax} in a Notebook level tagging rule.

Start by initializing the tag.

CurrentValue[EvaluationNotebook[], {TaggingRules, "testplot-x-span"}] = None;

To reestablish this initial value you will need to delete any running example of Testplot and re-evaluate the above code.

Now define the interactive tool.

Clear[Testplot]

Testplot[
    signals_: {__},
    {xmin_, xmax_, step_, {xname_String, xunit_String}}] :=
  DynamicModule[{initSpan},
    Manipulate[
      CurrentValue[EvaluationNotebook[], {TaggingRules, "testplot-x-span"}] = xSpan;
      ListLinePlot[signals,
        AxesLabel -> {xunit, None},
        DataRange -> {xmin, xmax},
        PlotRange -> {xSpan, All}],
        {{xSpan, initSpan, xname}, xmin, xmax, step,
          ControlType -> IntervalSlider,
          Appearance -> "Labeled",
          Method -> "Stop",
          MinIntervalSize -> step,
          ImageSize -> {300, 25}},
           ControlPlacement -> Bottom],
    Initialization :> (
      initSpan =
        CurrentValue[EvaluationNotebook[], {TaggingRules, "testplot-x-span"}];
       If[initSpan === None, initSpan = {xmin, xmax}])]

With this definition xmin and xmax will be remembered even over re-evaluation of calls to Testplot and even re-evaluations of Testplot that don't change the memoization part of the code.

Now call Testplot with data to see how it looks

signal1 = Table[t, {t, 0, 4, 0.1}];
signal2 = Table[t^(1/2), {t, 0, 4, 0.1}];
Testplot[{signal1, signal2}, {0, 4, 0.1, {"x-Axis", "seconds"}}]

init

Now reduce the domain span.

plot

Now edit the control label (from "x-Axis" to "x-span") and re-evalute Testplot.

Testplot[{signal1, signal2}, {0, 4, 0.1, {"x-span", "seconds"}}]

edited

The label to the left of the interval slider has changed, but the reduced domain is remembered.

|improve this answer|||||
$\endgroup$

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.