Questions on Mathematica's scoping constructs (Block, Module, With, etc.)

learn more… | top users | synonyms (1)

0
votes
1answer
45 views

Behavior of custom file chooser code changes when placed inside DynamicModule

My primary objective is to build a file chooser that aides a user in selecting files into one of two categories. Files can be individually selected, or based on the clicking of a specific checkbox, ...
5
votes
1answer
95 views

Dynamics and Block interfere

As mentioned by Albert Retey in a comment, we can unintentionally interfere with our dynamics as follows ...
0
votes
1answer
88 views

Basic (?) dynamic module questions (If clause and refresh problems)

I'm building a dynamic module, but I'm stuck with a few things i don't know how to deal with. In particular I'm stuck with the "If" sentence and the refresh inside the dynamic module. Here's a ...
1
vote
0answers
35 views

Module does not evaluate the same way as without [closed]

I have a module as follows: ...
1
vote
1answer
54 views

Module and Manipulate: Why the Red Highlighting? [duplicate]

If I define a function that returns a Manipulate object, and the Manipulate has a local variable inside of it, the local ...
0
votes
2answers
229 views

Use a Module to return multiple plots

A program I'm running requires me to generate multiple plots over the same domain, so I thought it would be fairly simple to use a module and bury all of the plot commands within that that way I only ...
3
votes
2answers
139 views

Memory Problem with Modules and NDSolve

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is: ...
1
vote
1answer
246 views

Combining Mandelbrot and Monte Carlo

I'm trying to combine plotting a Mandelbrot set with Monte Carlo randomization to plot an equation using random points for complex number z, for the function $z^3-2z+2=0$. Below is the code I have ...
1
vote
2answers
124 views

Random walk using Module [closed]

I want to create a random walk using Module. The walk start at {0, 0}. At every step, x ...
4
votes
1answer
110 views

Unwanted interaction between instances of a Manipulate function

The function trajectory below is defined in terms of Manipulate and calls the auxiliary function ...
3
votes
0answers
82 views

Decrease computation time of module [closed]

I need to compute the exponential of a matrix (in this caseR) by the summation method where R is a square matrix with symbolic ...
10
votes
1answer
281 views

Context of localised (dynamic) symbols

When you localise a symbol with Module, the created unique symbol is usually in the current context. There seems to be an exception to this which has further ...
14
votes
3answers
263 views

How does $CellContext` work?

The only official info about $CellContext` I was able to find is: placeholder for the context of a symbol inside Dynamic ...
17
votes
2answers
409 views

Messages leak out from Block

Bug introduced in 8.0 or earlier and fixed in 10.1 When assigning message to a symbol localized by Block, I would expect that this message is not accessible ...
2
votes
1answer
48 views

Local function becomes global after Plot [duplicate]

In the code below, I define and plot a local function f. Clear[f] Module[{f}, f[x_] := 1; Plot[f[x], {x, 0, 1}]] However, ...
2
votes
1answer
77 views

Writing a procedure that invokes Manipulate[]

I would like to write a procedure with the signature: Foo[vars, params] which produces a Manipulate object. The procedure ...
17
votes
2answers
511 views

I define a variable as local to a module BUT then the module uses its global value! Why?

I have a question about modules and local variables. Here's my example: h = 5; Module[{a, h}, a[h_]= h^2; a[4]] (*Out[2] = 25*) I expected the module to ...
4
votes
1answer
171 views

ControlType -> None Vs. Module inside Manipulate (i.e. making everything local)

I'm building a program on another one made available in the Wolfram Demonstration Project. I've noticed that in the original code (working flawlessly) many variables were made local inside the ...
5
votes
1answer
110 views

Module does not remove a function definition

The following is a minor variant of the function uniqueTuples as given by Simon Woods in this answer. ...
15
votes
2answers
360 views

What is the purpose of Internal`LocalizedBlock?

I have come across the (internal) use of of the function: Internal`LocalizedBlock I am trying to determine its purpose. It seem to behave like ...
5
votes
4answers
288 views

Replacing parts of a matrix

I'm a beginner to Mathematica and to writing scripts in general. I'm currently trying to write a code that will take any element in a matrix higher than a certain threshold value n and set it equal to ...
2
votes
1answer
44 views

Substitution breaking the scope

A novice's problem, perhaps: it seems that /. substitution does weird things to the scope. ...
2
votes
2answers
151 views

Expressions containing globally undefined symbols inside a function where they are defined

Consider the following code. f[a_,b_]:=x; Clear[a,b]; x=a+b; f[1,2] It returns a+b. However, from a certain viewpoint, one ...
1
vote
2answers
94 views

Localised variable shows up as Global [duplicate]

Why does this local variable show up as Global? ?q Information::notfound: Symbol q not found. >> ...
-7
votes
2answers
155 views

Module function corrupts string? [closed]

I have the module who should replace some elements of a matrix with checkboxes ...
2
votes
1answer
70 views

How to protect function variables [duplicate]

I am sure this has been asked before, but I can't seem to find the right solution. I want a function to be the solution to a differential equation ...
5
votes
2answers
229 views

A better way of handling Module output?

I am sure this has been asked before in a way or another. I need to Save or DumpSave some symbols that contain the result of ...
1
vote
1answer
53 views

Global variales, Paralell computation and Limiting notebook's contex

If I limit the context of each notebook: and use parallel computation: ParallelTable[ToExpression["b" <> ToString[j]], {j, 1, 10}] then after ...
9
votes
2answers
222 views

How to assemble and disassemble a function into steps

I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
0
votes
0answers
104 views

Best practices for breaking up functional blocks?

Say I have a main function that does some job, and I'm only interested in the final result of ans, i.e., the local functions are not important and produce no ...
3
votes
1answer
72 views

Should “counters” used in Do, Table, … within Module be declared as local?

I have little to no understanding of how Mathematica handles scoping, so the question may seem a little bit funny. What is the preferred way: ...
3
votes
2answers
244 views

Strange interaction between Module and ListPlot

Bug introduced in 10.0 and fixed in 10.1 Why is the variable a ending up assigned to {1,2,3} after executing Module[{a}, a=Range[3]; ListPlot[a] ]
1
vote
1answer
205 views

NDSolve inside Module with local variables

I want to use NDSolve within a Module and output the solution at some times. (Mathematica 10) All works fine with global variables (symbols) ...
4
votes
1answer
114 views

Defining a function that prints both the name and value of a variable defined locally within the calling function

To save time when debugging with Print statements, I'd like to define a function -- call it PrintVal -- that prints both the name and value of a variable defined locally within the calling function. ...
8
votes
1answer
313 views

A smarter nested With?

I often find myself writing code that looks a bit like this: ...
3
votes
1answer
134 views

Hiding body of a module or function in mathematica

Is it possible to hide the body of a module or function in MMA? For example I would like to turn this: myModule[]:=[{}, Body of module ] to: ...
4
votes
2answers
99 views

How do I localize an assignment to expression (instead of symbol) within scoping construct?

Is it possible to localize an assignment to an expression (defined via UpSet) instead of a Symbol within a scoping construct? ...
12
votes
1answer
233 views

Temporary variable not removed in Mathematica 10

Bug introduced in 10.0.1 and fixed in 10.0.2 In Mathematica 10, the documentation still states that a symbol with attribute Temporary will automatically be ...
1
vote
2answers
88 views

Assigning to the “local” version of a symbol

Compare Manipulate[c[1] = 1; c[1], {c[1], 0, 1}] with Manipulate[Do[c[i] = 1, {i, 1}]; c[1], {c[1], 0, 1}] The first case ...
4
votes
3answers
325 views

Altering values in Manipulate with dynamically generated controls

I have a Manipulate whose controls are generated based on a parameter in a containing Manipulate: ...
0
votes
2answers
88 views

Use conditional expression inside With

I want to assign a number to an internal variable, depending if a specific option has been given, but it seems to fail. Something like: ...
2
votes
1answer
128 views

Odd problem with Module

I've been having a strange problem with Module. Oddly it seems to be linked to my use of Import and then applying a function such as Flatten to my Import. I pasted images of my code and output ...
1
vote
2answers
67 views

Is there a way to logically test an optional color input inside a module? [duplicate]

Suppose I create a Module that is expected to return a graphic that I will later use in a plotting procedure (e.g., with ...
0
votes
1answer
104 views

Substitution of a function in a differential equation

Suppose we have a differential equation as follows: g[t_] := t/2 m[t_] := t/3 A[t_] := {{g[t], 1}, {1, m[t]}} p'[t] == A[t]. p[t], p[0] == {1, 0} ...
3
votes
1answer
110 views

Evaluating arguments of module (inside compile)

Note: this is a clarification of 2 earlier unanswered questions, Compile issues, scoping and order of evaluation and Optimising a simple computation with vectors . The problem I have a list of ...
2
votes
1answer
122 views

Manipulating parameters in a complex algorithm

I have a complex algorithm that depends on: A wavelet basis A number of sampling points out of a distribution A thresholding value I want to be able to play with different scenarios using ...
3
votes
0answers
117 views

Why is it not allowed to define vectors of variables in a scoping construct?

The following command assigns 1 to a, 2 to b and 3 to c: ...
2
votes
0answers
102 views

Compile issues, scoping and order of evaluation

I have a few questions about compiling functions, which I think are all related to scoping and order of evaluation. I will illustrate them by a minimal example of the problem I have. I'm sorry for ...
0
votes
1answer
71 views

Output a symbol from Module or Block

I have troubles to output an entire symbol, a function in this case, from a module or a block. For instance myf = Block[{f}, f[3] = 33; f[4] = 4; f] Then I want ...
1
vote
0answers
133 views

Building Reusable Custom Controls?

I would like to build an application using Mathematica 10. I would very much like to structure the application for maximum maintainability. To this end I would like to segregate controls and user ...