2
$\begingroup$

I have 60 txt files which I want to import them into Mathematica using a Do loop. Each file contains some numbers (more precisely, a list of numbers). I want to put the content of each file in a variable with a name that is the same as the original file. Now, the problem is, I have no idea how to build a variable automatically with the same name in each loop run.

To make things clear, I wrote the following sample code

SetDirectory[NotebookDirectory[]];
(* reading all files in the directory *)
all = FileNames[]
Do[
 name = StringSplit[all[[i]], "_"][[1]];
 NUMkinPmat = ToExpression[Import[all[[i]], "Lines"]];
 , {i, Length[all]}]

Where all[[1]] is NUMkinPmat_1836.txt for example, namely the name of the first file. I want to build a variable called NUMkinPmat. The name gets what I'm looking for (NUMkinPmat), however, I don't know how to put all[[i]] in it, so wrote it manually in the above code. I can't write in this way also:

ToExpression[StringSplit[all[[1]], "-"][[1]]] = 
  ToExpression[Import["VARkinPmat_1836.txt", "Lines"]];

because it returns Tag ToExpression in ToExpression[NUMdenE_1836.txt] is Protected. error. I hope I got my point across. Any idea?

$\endgroup$
4
  • 2
    $\begingroup$ In general I don't think it's a good idea to encode information in the variable name. Better to use something like data["NUMkinPmat_1836.txt"] = Import["NUMkinPmat_1836.txt", "Lines"] etc. In this way you can look up the data by referencing the file name as a pseudo-argument to the data symbol. $\endgroup$
    – Roman
    Dec 26, 2022 at 15:26
  • $\begingroup$ As stated by @Roman, this is generally a bad idea. If you really insist, take a look at Symbol. You can do something like: Evaluate@Symbol[name] = NUMkinPmat $\endgroup$
    – Domen
    Dec 26, 2022 at 15:28
  • $\begingroup$ @Roman Thanks, but when I try your code by ToExpression I get error. $\endgroup$
    – Wisdom
    Dec 26, 2022 at 15:32
  • $\begingroup$ @Domen I don't insist in my idea, but give me a better method to recall 60 imported files in the next calculations. Sorry I didn't got your mean about symbol, can explain more? $\endgroup$
    – Wisdom
    Dec 26, 2022 at 15:39

2 Answers 2

2
$\begingroup$

In this example data is cleared in advance for reruns. Clear[name] clears name not data so Apply is used.

filename = "data.txt";
name = First[StringSplit[filename, "."]];
Apply[Clear, List[name]]
With[{x = ToExpression[name]}, x = Import[filename, "Lines"]]

data

1,2,3

You can also use Symbol instead of ToExpression.

all = FileNames[]
Do[
 name = StringSplit[all[[i]], "_"][[1]];
 Apply[Clear, List[name]];
 With[{x = Symbol[name]}, x = Import[all[[i]], "Lines"]],
 {i, Length[all]}]
$\endgroup$
2
  • $\begingroup$ Thanks a lot. I replaced name = all[[i]]; vname = StringSplit[name, "_"][[1]]; by yours and put it in the loop, and it worked! It just needs an additional ToExpression command when I when plot using data. Can explain more about the line started by With? $\endgroup$
    – Wisdom
    Dec 26, 2022 at 16:49
  • 1
    $\begingroup$ Usefully in this case With has the attribute HoldAll which enable the variable name to be handled. See the details section. $\endgroup$ Dec 26, 2022 at 16:54
1
$\begingroup$

I would suggest two aspects of your code: (1) don't encode information in variable names, as commented above, and (2) don't read everything in a Do loop but rather lazy-read on demand. Concretely, using a memoizing field data:

data[name_String] := data[name] = Import[name, "Lines"]

In this way, every time you ask for a specific data, the content is memoized (buffered) in the kernel. For example,

SetDirectory[NotebookDirectory[]];
all = FileNames[]

data[all[[1]]]
(*    the data as read    *)

reads the file only when called for the first time; at later times the cached (memoized) data are used. So everything can be read lazily (on-demand) without pre-reading everything in a Do loop.

$\endgroup$
1
  • $\begingroup$ How to implement this method with ToExpression because I need to this commend to convert my data to correct ones. $\endgroup$
    – Wisdom
    Dec 26, 2022 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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