MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Using variable name as string AND value ?

  • To: mathgroup at
  • Subject: [mg107052] Re: Using variable name as string AND value ?
  • From: David Bailey <dave at>
  • Date: Mon, 1 Feb 2010 06:14:45 -0500 (EST)
  • References: <hk17o6$ojp$>

Bill Rowe wrote:
> On 1/29/10 at 7:47 AM, pkshreeman at (Paul) wrote:
>> In process of writing a certain routine, I am puzzled to how to
>> write this concept into Mathematica format.  What I need to do is be
>> able to extract both string and values, p1 as parameter number one,
>> p2 as parameter number 2, etc...and I have to put all values into
>> one single matrix to do some mathematical operations.  However, I
>> also have to call a function that depends on using the variable
>> p1,p2, so I have to reassign the values back as following
>> p1=10 p2=23 ...
>> Parameter={p1,p2,......}   and perhaps Strings={"p1","p2",...}?
>> After mathematical manipulations of matrix Parameter is done, I can
>> manually do this
>> p1=Parameter[[1]], p2=Parameter[[2]], ....
>> But I need to be able to do it with Do Loop such as
>> Do[ String[[j]]=Parameter[[j]] ,{j,1,PL}]
>> The problem is that this approach doesn't work in Mathematica.
> Here is a way to do what you want. First I will create a list of
> parameter names and values to be assigned to them
> In[13]:= parameterNames = {"a", "b", "c"};
> parameterValues = {1, 2, 3};
> Then I will assign the values to the paramters
> In[15]:= Evaluate[ToExpression[parameterNames]] = parameterValues;
> and to show the parameters have the assigned values
> In[16]:= {a, b, c}
> Out[16]= {1,2,3}
> Now, this works as written only if the parameters {a, b, c} do
> not exist at the time the Evaluate[... is executed.
> What I am doing is using ToExpression to convert the list of
> strings (parameter names) to symbols Mathematica can assign
> values to. I use Evaluate to force creation of a list of
> symbols. Without the Evalaute, Mathematica will issue a error
> message saying you cannot assign values to ToExpression.
> =46inally, the = operation assigns the values. It is not necessary
> to use a looping construct such as Do since Set ("=") threads
> over its arguments.

The following command will clear any existing parameter values so that 
Bill's code will work even when the parameters do have values.

Map[Clear, parameterNames]

David Bailey

  • Prev by Date: Re: newbe question about Sting Comparison
  • Next by Date: Re: How to combine graphics pimitives and Plot function?
  • Previous by thread: Re: Can Mathematica interpolate non-uniform scatter data?
  • Next by thread: Re: Re: Re: position of sequence of