Message Boards Message Boards

0
|
3543 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Storing data in variable name depending on loop index?

Posted 2 years ago

I have 30 separate data files having similar kind of names. Each file has 100 rows and 3 columns. I have read all the files together using FileNames and finally I have stored all the data in a single variable status. So status has dimensions {30,100,3} I now want to store data as a1 = status[[1,All,3]], a2 = status[[2,All,3]] ...ai = status[[i,All,3]]. How to store the data individually in variables whose name will depend on index value?

POSTED BY: Kasturi Banerjee
3 Replies
Posted 2 years ago

You could consider using indexed variables. Instead of naming the variables a1, a2 .. ai, name them a[1], a[2] .. a[i]. The index number in square brackets can then easliy be assigned from code. And the value of a[n] can likewise easily be retrieved.

enter image description here

POSTED BY: Hans Milton

Something like this?

In[28]:= vars = Table[Symbol["dddd" <> ToString[i]], {i, 1, 10}]

Out[28]= {dddd1, dddd2, dddd3, dddd4, dddd5, dddd6, dddd7, dddd8, \
dddd9, dddd10}

In[29]:= values = Table[i*3, {i, 1, 10}]

Out[29]= {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}

In[31]:= MapThread[Set[#1, #2] &, {vars, values}]

Out[31]= {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}

In[33]:= vars

Out[33]= {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}

or in one line, you need to first evaluate the Symbols:

Table[Evaluate[Symbol["dddde" <> ToString[i]]] = i*3, {i, 1, 10}]

Regards

POSTED BY: Neil Singer

Welcome to Wolfram Community!

Please include your code and output using one of the methods explained in the rules: http://wolfr.am/READ-1ST

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

Group Abstract Group Abstract