3
$\begingroup$

I want to convert strings to variables. I want to create the term pionprodg1. I tried to do this:

particula ="pion";
ToExpression[particula <> "prodg1"]=MyTable1;

But there is a error:

Set::write: Tag ToExpression in ToExpression[pionprodg1] is Protected.

How can I solve this?

$\endgroup$
1
  • 2
    $\begingroup$ Look up "Symbol" in the help $\endgroup$ Feb 27, 2023 at 19:43

2 Answers 2

5
$\begingroup$

Try with this:

ToExpression[
    particula <> "prodg1",
    InputForm,
    Function[f, f = MyTable1, HoldAll]
]
$\endgroup$
1
$\begingroup$

The resource function ConvertStringsToSymbols is useful for this, but you need to use With to evaluate the symbol names:

With[{
  sym = "pion" <> "prodg1"
  },
 ResourceFunction["ConvertStringsToSymbols"][sym = 1, sym]
]
$\endgroup$

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