Automatically create array of named variables

  • Context: Mathematica 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Array Variables
Click For Summary
SUMMARY

This discussion focuses on automating the creation of named variables in Mathematica for use in the Manipulate function. The user explores the use of indexed variables through the command P = Table[Indexed[p, i], {i, 1, 7}] to generate a list of variables {p1, p2, p3, p4, p5, p6, p7}. However, challenges arise when attempting to feed these variables into Manipulate as a list of locators. The conversation highlights the potential of using ToExpression and StringRiffle to address these issues.

PREREQUISITES
  • Familiarity with Mathematica's Manipulate function
  • Understanding of indexed variables in Mathematica
  • Basic knowledge of ListPlot in Mathematica
  • Experience with generating random numbers using RandomReal
NEXT STEPS
  • Research how to use ToExpression in Mathematica for dynamic variable creation
  • Explore the StringRiffle function to concatenate strings effectively
  • Learn about advanced usage of the Manipulate function with variable arguments
  • Investigate the creation of dynamic lists in Mathematica for graphical representation
USEFUL FOR

This discussion is beneficial for Mathematica users, particularly those involved in data visualization and dynamic interface creation, such as data scientists, educators, and researchers looking to enhance their programming efficiency in Mathematica.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
786
In the following example...
Code:
Manipulate[
             ListPlot[{p1, p2}, PlotRange -> {{0, 3}, {0, 3}}, Joined -> True],
             {{p1, {0.5, 0.5}}, Locator},
            {{p2, {1, 0.7}},   Locator}
         ]

... we have created variables p1 and p2 that are local to the Manipulate block, and we use them to set up two locators on the ListPlot. Now if we want to create a large number of locators, we could manually type in a large number of p1, p2, p3, p4 ... and so on. But is there a way to build these arrays of named variables programmatically?

The above is just one example, but there are other constructs in Mathematica where an input to a function must contain multiple named variables. It would be nice to automate the creation of large lists like that.
 
Physics news on Phys.org
A bit of googling turned up the concept of indexed variables. For example, we can do :
P = Table[Indexed[p, i], {i, 1, 7}]

which gives us ##P = \{ p_1, p_2, p_3, p_4, p_5, p_6, p_7 \}## , and we can use these as our seven variables.

This takes us one step forwards... But unfortunately, when we look at the locator specification at the end, it doesn't have a true list like format. It's just a series of extra arguments to the Manipulate. So the question turns into, "how can we feed in multiple arguments into a function automatically, when it can accept a series of arguments?"========== Edit: I realized that the indexed variables don't work as I thought ===
 
I'll look them up and give them a try... thanks.
 
ListPlot will iterate over a list of expressions:

Code:
P = Table[Indexed[p, i], {i, 1, 7}]
pointList = Table[{RandomReal[{0, 3}], RandomReal[{0, 3}]}, {i, 1, 7}]
Manipulate[
ListPlot[P, PlotRange -> {{0, 3}, {0, 3}},
  Joined -> True], {{P, pointList}, Locator}]
 
  • Love
Likes   Reactions: Swamp Thing
cabrera said:
ListPlot will iterate over a list of expressions:

Really neat. Thanks!

I had tried unsuccessfully to get Manipulate to accept a list of locators, but I gave up as I couldn't figure out the right syntax... and I wrongly concluded that it just isn't designed to take a bracketed list.

Even now, your code has some subtlety that I can barely wrap my head around.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 178 ·
6
Replies
178
Views
9K
Replies
0
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
10K
  • · Replies 4 ·
Replies
4
Views
15K