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!
Module[{x}, x]
. At each new evaluation, independently of the cell, you will get a newx$...
symbol. Do you want the evaluation in the first cell to always give the samex$...
, and the same for the second cell with anotherx$...
? $\endgroup$ – user31159 Sep 17 '16 at 16:32Echo
at the first occurence ofxInt
andlists
within the module. So something likeEcho[xInt]
for instance. This will print the value ofxInt
. You could also use a global variable to store their values, soxIntGlobal = 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:43Echo
suggestion later today. $\endgroup$ – Wilson Sep 17 '16 at 16:52