Group Abstract Group Abstract

Message Boards Message Boards

Clearing symbols on the fly

I am writing modules that need to create symbolic vectors and matrices on the fly, with cleared entries. I noticed the following problem:

 Clear[w1]   or  Clear ["w1"]    both work as per doc

 Clear[Symbol["w"<>ToString[1]]]   or  Clear[ "w"<>ToString[1]]   do not

Actually 1 is a running index in a Table being created. For example,

 symbolicVector[letter_,n_]:=Module[{i,v=Table[0,{n}]},
  For [i=1,i<=n,i++, vi=Symbol[letter<>ToString[i]]; Clear[vi]; v[[i]]=vi]; Return[v]];

Here symbolicVector["w",4] should return {w1,w2,w3,w4} with all entries properly Clear'ed. In my first implementation I used only Table; the above (entered here by hand) is to make easier to insert prints inside the loop. Is there a way to make this work? Thanks.

POSTED BY: Carlos Felippa
2 Replies

Hi Sander,

Thanks! That solves my problem.

POSTED BY: Carlos Felippa

Hi Carlos,

Clear and ClearAll both have attributes HoldAll (Attributes[Clear]), meaning that it does not evaluate it's arguments. So it tries to clear "w"<>ToString[1] as a whole. You have to explicitly evaluate it before executing the function like:

w1=3
Clear[Evaluate["w"<>ToString[1]]]
w1

Hope that helps.

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard