How do I assign variable names from a list of strings?
- To: mathgroup at smc.vnet.net
- Subject: [mg103029] How do I assign variable names from a list of strings?
- From: Tyler <hayes.tyler at gmail.com>
- Date: Fri, 4 Sep 2009 03:16:49 -0400 (EDT)
Hello All:
As a part of a weekly procedure, there are several tsv (tab separated)
files I want to load into Mathematica, process, and then store in a
native format (.dat) for more analysis later on. So far, this is what
I've come up with.
[1] First I get all the files in the directory data I want to load in
tsvfiles = FileNames["data/*.tsv"];
tsvfiles
{"data/abx.tsv", "data/aem.tsv", "data/agu.tsv", \
"data/bmo.tsv", "data/bns.tsv", "data/cm.tsv", "data/cnq.tsv", \
"data/eca.tsv", "data/g.tsv", "data/imo.tsv", "data/k.tsv", \
"data/mfc.tsv", "data/pbg.tsv", "data/pot.tsv", "data/ry.tsv", \
"data/su.tsv", "data/td.tsv", "data/tlm.tsv"}
[2] I then create names I want for the variables so I can further
process the TSV files and store in native Mathematica format.
varnames =
StringReplace[#, {"data/" -> "px_", ".tsv" -> ""}] & /@ tsvfiles;
varnames
{"px_abx", "px_aem", "px_agu", "px_bmo", "px_bns", "px_cm", \
"px_cnq", "px_eca", "px_g", "px_imo", "px_k", "px_mfc", "px_pbg", \
"px_pot", "px_ry", "px_su", "px_td", "px_tlm"}
[3] I import an example (this worked!)
data = Import[tsvfiles[[1]], "TSV"];
Dimensions[data]
{1142, 6}
[4] Last, I automatically assign the variable name based on the where
I am in the loop.
varnames[[1]] = data;
So, this is the part I'm not getting right. Is there a way to change
"varnames" into a list of variables I want to assign data to? Or is it
enough to just use the varname[[1]] (for example) as the newly
processed and formatted filename I now want to store? In other words,
storing a variable does not preserve the original name of the variable
when loaded, but rather is re-assigned when imported later on?
Cheers,
t.