Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to convert a list of string names into symbols, which will then be used to store data. I have 24 files (where the name of each file is a member of the list mentioned above) that I need to process, which is why I am trying to accomplish my goal progammatically. The code below results in a Tag warning. I have read the normal documentation sources, and appreciate that you can not inappropriately assign something to a protected symbol, but this doesn't help me solve the problem.

Would you have any advice how to accomplish my goal?

Table[(ToExpression[mmsignalnames[[i]]] = 
   Extract[ToExpression[celfilenames[[i]]], mmammindices[[j]]]), {i, 
  1, Length[mmsignalnames]}, {j, 1, Length[mmammindices]}]

Set::write: Tag ToExpression in ToExpression[mmsignalGSM356796] is Protected. >>
share|improve this question
Creating many different symbols is an error-prone practice, because you need ways to manage them, which is hard in this case. What you seem to be needing is a hash-table, with keys being your names (as strings) - this is the second suggestion in the answer of @Mr.Wizard. – Leonid Shifrin Jan 26 '12 at 23:19
Hello Todd and welcome to the Mathematica StackExchange. Don't forget to upvote good answers (and other people's questions) using the triangle above the number next to the post, and use the checkmark to "accept" the answer to your question that you think best answers it. You now have enough "reputation" (points) to visit the chat room and chat if you would like. – Sjoerd C. de Vries Jan 26 '12 at 23:20

5 Answers 5

up vote 23 down vote accepted

One solution is to use the third argument of ToExpression:

With minimal modification, a working version of your code would look like this:

    name = Extract[ToExpression[celfilenames[[i]]], mmammindices[[j]]],
 {i, Length[mmsignalnames]}, {j, Length[mmammindices]}]

(Untested because I don't have your data; but see below for the main idea and a small demonstration.)

The core of the method is this:

ToExpression["a", InputForm, Function[name, name = 1, HoldAll]]

ToExpression will wrap the result into its third argument before evaluating it. We can make the third argument a function that sets a value to the symbol (in this simple example it always sets the value to 1). HoldAll is needed to make sure the symbol won't evaluate when it is passed to the function.

You might find all the evaluation control I'm using here a bit confusing. To learn how to work with unevaluated expressions, I recommend reading

It is one of the best tutorials on the matter.

Finally, after answering your actual question, I'd like to suggest you use a hash table instead of symbols:

Instead of creating symbols from the strings "a", "b", "c", ..., and assigning to them, you could assign to myTable["a"], myTable["b"], ... This will make programmatic access to this data trivial. You won't need to bother with evaluation control nearly as much. And more importantly, you can avoid accidental name collisions with existing symbols. Here's an example:

(myTable[#] = 1) & /@ {"a", "b", "c"}
share|improve this answer
The link to the Villegas tutorial is now – Michael E2 May 11 at 15:47
@MichaelE2 Thanks! Feel free to make the correction next time! – Szabolcs May 12 at 21:58

You need to make the left-hand side of Set a symbol at the time of evaluation. Use With or similar to inject the symbol:

mmsignalnames = {"one", "two", "three"};

With[{lhs = Symbol[mmsignalnames[[2]]]},
 lhs = 5


Another approach that could be important if you are trying to make assignments to symbols that already have values is this:

Function[{lhs}, lhs = 7, HoldAll] @@ MakeExpression[ mmsignalnames[[2]] ] ;


Or a bit more terse using the "injector pattern":

MakeExpression @ mmsignalnames[[2]] /. _@x_ :> (x = 9);


Also, it bears mentioning that one may also use indexed symbols such as

name["two"] = 5;


For another approach to this kind of problem see:

share|improve this answer
In the first case when you know your symbols haven't been defined yet, you could shorten it with Evaluate[ToExpression[mmsignalnames[[2]]]]=5 – Rojolalalalalalalalalalalalala Jan 27 '12 at 15:51
@Rojo yes, you can; I think that With is more flexible however, and I thought it would be easier to use in practice, though I admit I did not actually try it both ways (I would prefer indexed symbols myself). – Mr.Wizard Jan 28 '12 at 1:58

In Mathematica version 10, you can also use Inactive to allow the Symbol to be created before doing the assignment.

Here is an example:

Activate[Inactive[Set][Symbol["x"], 3]]

(* ==> 3 *)


(* ==> 3 *)
share|improve this answer

So recently I've learned from John Fultz that RawBoxes are kind of verbatim indicator for MakeBoxes which is not well stressed out in documentation.

This or I've missed the point but it doesn't matter, here we have handy way to do this:

x = 5;
ToExpression @ MakeBoxes[RawBoxes["x"] = 123];
share|improve this answer
+1 for an exceptionally clear illustration of RawBoxes – Mr.Wizard May 11 at 8:26
@Mr.Wizard My life would have been waaay more easier if somebody have shown me this 2-3 years ago :( But I'm glad I've found and understood this. I think this is one of things I was missing in a long time. Thanks for +1 :) – Kuba May 11 at 8:28

This answer is in the same spirit as Kuba's answer, but it avoids conversion between boxes and expressions.

Here is our workhorse

mark::usage = "Use in combination with markedExpression";
markedExpressify::usage = 
  "Use mark to mark strings for conversion to expressions";
markedExpressify[expr_] :=
 Module[{pos, hCExpr, ext, thr, rLH, expresser},
  hCExpr = HoldComplete[expr];
  pos = Position[hCExpr, Unevaluated@mark[_] ];
  expresser =
    ToExpression @@ {Unevaluated @@ #, InputForm, HoldComplete},
  ext = Join@@Extract[hCExpr, pos, expresser];
  thr = Thread[RuleDelayed @@ {HoldComplete @@ pos, ext}, 
  rLH = List @@@ Hold@Evaluate@thr;
  First[ReplacePart @@ {hCExpr, Unevaluated @@ rLH}]

We can then do something like

markedExpressify[Unevaluated[mark[#] = f[#] = 123]] & /@ {"y", "x"};

It works even if mark has a value, though this is something you should probably avoid.

mark = 6;
markedExpressify[Unevaluated[mark["z"] = 5]];
markedExpressify[Unevaluated[mark["g[2]"] = 3]];

I did not give the function attribute HoldAll, because this way it works more nicely with held expressions.

markedExpression = Hold[mark@"x" := f@mark@"y" = 5];
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.