# Questions tagged [scoping]

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

579 questions
Filter by
Sorted by
Tagged with
1 vote
67 views

### How to write a function that if called within another function will check user authorization and otherwise not execute the rest of code

I tried the following: ...
• 9,533
52 views

### Why do notebooks by default put symbols in Global context where they can collide with all other notebooks?

This default setting can cause subtle and difficult to find problems, such as inconsistent or incorrect answers when symbols, variables or functions in different notebooks have the same name. I speak ...
• 31
56 views

### Problem when code is inside module

I have this code: ...
50 views

### Function returning error every other run

Disclaimer: I am new to Mathematica but have some experience with other programming languages. I have defined the following function (simplified version): ...
1 vote
69 views

### Problem defining Function inside Module to verify ODE solution [duplicate]

I modified the question. It turns out this problem has nothing to do with using a package. New style or standard style./ Same problem shows in a normal module in notebook. This is the most bizzar ...
• 148k
67 views

### Defining a Function that Can Optionally Output an Arbitrary Number of Tables and Graphs

I'm importing some data from a csv file, performing various filtering operations on it, and then constructing graphs. I'm trying to define a function that does it 'all,' that supports the importing, ...
• 73
46 views

### How to fix parameter locally for iterating recursion equations

The following is a simplified version of a more detailed problem. I have two coupled recursion equations of two variables, x and y. One equation also depends on a parameter, c: ...
• 393
49 views

### How can I implement dependence between locally defined variables in With? [closed]

I have a procedure that randomly selects a list element and compares the element value to the value of its position. If a condition is met, the element is replaced. The following works ...
• 393
100 views

### How to Block built-in functions conditionally in a single Block statement? [closed]

I have used the following method and it seems to be working. Just posting here since this way seems hacky. So if anyone can point a better way. ...
• 9,533
57 views

### Programmatically establishing controllers for Manipulate inside a module

I am trying to create a module that allows users to cycle lists to look for a pattern and need a way to programmatically establish the number of controllers depending on the number of lists being used....
62 views

### Why do I get a wrong data structure using Module? [closed]

I am using Module and reading rawlen.txt files from nb100, nb200 , ... folders. I have written a piece of program to calculate strain and I expect the results to have the following structure: {{0.}, {...
51 views

### How does the scoping in Mathematica work using with blocks? [duplicate]

I'm attempting to understand the following scoping issue: ...
48 views

### Substituting solution into an equation in a module

I have this model which gives me list as an output ...
157 views

### Evaluation control of nested With, If and Integrate

Can you explain the behaviour of the following? The first With outputs unevaluated If. So in the second ...
• 188
79 views

### Red colored font in code

To avoid affecting lamda in other part of my notebook, I have wrapped code with Module. After wrapping, red colored font appeared.Could I know the reason? ...
• 648
507 views

### What's the difference between With and ReplaceAll?

A wolfram Tutorial mentions that ReplaceAll and With are similar: The way ...
• 1,055
58 views

### Writing 'yield' style generator function

Just out of personal curiosity because I can't think of any solution. Probably, it's not possible, but I want a decisive answer. Is there some clever way of implementing the following function? ...
• 7,921
1 vote
83 views

### Define list of local variables to be parsed to Module

I have a Module which has $n$ local variables, say $a_i$. Now I want to write my Module in such a way that I do not need to change the code for varying $n$. My approach is ...
1 vote
83 views

### SortBy does not work correctly when nest in Evaluate in With in Block

I have got some formula in Mathematica, then I want to pass value to verify them. full code ...
• 197
152 views

### With function replace rule does not work in

If I define an expression , and evaluate it in With function, replace rule does not work. sumT = TA + TE; With[ {TA = 0.1, TE = 0.2}, {TA + TE, sumT} ] output <...
• 197
60 views

### Using a module that turns the output of Findroot into a function of a variable

I am trying to create a module that finds the root b* of some equation with two variables b and ...
91 views

### Using Dynamic string output inside Block

Inside a Block, I have (among other things) a string output that is updated during an internal for loop in the following (schematic) way: ...
103 views

### Using Manipulate for initial guess to nonlinear fitt, feed the result back to Manipulate (and repeat) for a general model with indexed parameters

I have been trying to put toghether a visual method to help fit experimental data with peaks. I have stumbled upon this answer which is very similar to where I got. But it is not sufficiently general ...
• 4,006
1 vote
66 views

### How to extract manipulated indexed variables from Manipulate

I am struggling with getting values of variables that are manipulated with Manipulate: The code: ...
• 4,006
41 views

### Manipulate with With and without - scope of variables

I am struggling with some basic concepts: Why this works: ...
• 4,006
81 views

### Temporary symbol leaks into public context?

This phenomenon has confused me a long time. Consider the following package example, ...
• 2,443
56 views

### how to use subscripted variable such as a_i as the control of Manipulate?

I'd like to define a series of variables called $l_1$,$l_2$, etc., in Mathematica, using the "ctrl+_" shortcut. Moreover, I want to use these variables as the controls of a Manipulate[] ...
• 735
97 views

### Interactive control panel using dynamic is surprisingly slow

I am hoping to create a user interface for performing gaussian fits on two dimensional data. I made a basic grid of various controls and labels. The tool works, but it's much slower than I anticipated,...
1 vote
83 views

### Dynamic text input of equation for graphing

I'd like a simple interface where the user can input (by text) a function $f(x)$, and then the function, its even part, and its odd part are all plotted on the same axes. I'd also like PlotLegend to ...
• 41.8k
51 views

### How to make this bisection code work for different equations?

I am very new to Mathematica. I have this code of Bisection Method for finding a root of a polynomial. ...
• 219
81 views

### Most efficient way to define several local constants in a function -- Block or nested Withs?

Sorry if this is kind of a noob question but I'm wondering what the best-performing approach is to the following: I'd like to define several local constants in the body of a function, each of which ...
• 111
78 views

### With[{a=1},{b=a},b] in 13.2 does evaluate to 1 [duplicate]

This isn't documented, and the front end highlights the second comma and third argument in red, but it's used in DownValues[Once][[3]] and is very convenient. Anyone know more?
• 139
1 vote
65 views

### Function definition inside Module with local symbol

Usually if I want to define a function using an expression I do: expr=x^2; f[x_]=expr; which usually works and give f a defination. But now if I want to do the ...
• 151
97 views

### Passing control values out of nested Manipulates

I am building a "batch photo cropper" UI. I have a list of images to crop, and I can use Manipulate to make a nice cropping UI for each one: ...
• 1,641
423 views

### Is this buggy behavoir of Module solved? [closed]

I have just found out about this bug of Module, i.e. the creation of "zombie"-variables in some cases (see also this post). I couldn't find a post saying ...
• 6,757
1 vote
85 views

### how to achieve “for(int i,i<=3,i++){}” of C++ in Mathematica [closed]

when i use Mathematica to do some small programmings, i always need to pay attention to variables-namings, i just wonder is there a way to achieve “for(int i,i<=3,i++){}” of C++ in Mathematica. ...
• 353
1 vote
127 views

### How to convert a function with named arguments to a pure function?

A function is defined with named arguments. How can this function be converted to a pure function that uses slots (#). As a simple example, how can the following: <...
• 239
72 views

### Problem with functions defined in Module in mathematica package

Might be very naive; I am trying for the first time to develop a Mathematica package as shown below. The intended usage is that the user will load the package, specify the input functions 'Model' and '...
89 views

### Finding the output of the nested CompoundExpression

I am trying to catch the output expression of a nested CompoundExpression like this ...
• 1,255
146 views

### What is the usage of other "*Block" and "With*"? [closed]

There exist some "kernel functions" that are related to the built-in scoping constructs: Assumptions`AssumptionsBlock, ...
• 505
500 views

### Why does Module slow down StringMatchQ by 180 times?

I knew that Module is slower than With or Block, often by several times. But it surprises me ...
• 2,443
1 vote
60 views

### Variable replacement not working in ParallelTable

The following code produces different results using Table and ParallelTable. K and ...
• 4,738
1 vote
107 views

### Module returning Null even with Module as a Return parameter

I'm currently trying to implement Newton's method of approximating roots to approximate the root of 2. I looked into some possible reasons as to why it would return Null, such as the scope of the ...
306 views

### How can I replace the locally defined function inside If with its value?

Consider the following module: ...
• 5,895
105 views

### Dealing with locally defined functions in Module while exporting/importing

Consider the following example module: ...
• 5,895
243 views

### about Return value of Module[]

questions are in the below code. Although I'm pretty sure of the answer, I still feel a little uncomfortable, because I feel that the format of the body of Module[] ...
• 353
1 vote
87 views

### Local variables in a module don't show their actual values

I need to do the following transformation that pick[a,b] -> ex[c|->{c[a],c[b]}] ex is a function that accepts a function as parameter which returns a list ...
• 307
71 views

### problem using package to verify solution of ode with Association, Naming scope/context handling [duplicate]

I learning how to use packages. I found a big problem. Here is description of the problem is, then a MWE is given. This small package takes in an ode and ...
• 148k
91 views

### Always the same function in function table

Let's say, we have a list of points such as Points = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; This is nothing but the corners of a square. List Plot: Now I ...
• 205