[Date Index]
[Thread Index]
[Author Index]
Re: Using variable name as string AND value ?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg106983] Re: Using variable name as string AND value ?
*From*: Bill Rowe <readnews at sbcglobal.net>
*Date*: Sat, 30 Jan 2010 07:13:24 -0500 (EST)
On 1/29/10 at 7:47 AM, pkshreeman at gmail.com (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.
Prev by Date:
**Re: looping**
Next by Date:
**Re: Using Export to create a spreadsheet containing formulas**
Previous by thread:
**Using variable name as string AND value ?**
Next by thread:
**Re: Re: Journals dying?, apparently rather slowly (was**
| |