2
$\begingroup$

There are some files in a directory. I have used of SetDiroctory to access all of them.

SetDirectory["C:\\Users\\SE7EN\\Desktop\\mathematica\\test"];

I can visit all theirs contents with

filenames = FileNames[]
{"Ronaldo.txt", "Messi.txt", "Beckenbauer.txt", "Zeydane.txt"}
loaddata[filenumber_] := Import[Part[FileNames[], filenumber]]

For example:

loaddata[4]     2    8    7     13   77

These are contents of Zeydane file. But I want to have imported files with the respectively devoted names. For example, desired result after that process, (which I do not know how it can be done), be same as bellow:

Ronaldo={2,3,5,8,0,1}
Messi={21,2,45,6}
Beckenbauer={11,42,5,2,7,21}
Zeydane={2,8,7,13,77}

One way is: importing them separately, Messi=Import{.....,'data'}. But it is not the desired process, The desired process must be done automatically. Automatically importing data from directory and devoting their names in that directory to the imported list.

Thanks in advance.

$\endgroup$
3
  • 3
    $\begingroup$ FileBaseName is useful to remove file suffixes. $\endgroup$
    – Yves Klett
    Jun 4, 2014 at 14:36
  • $\begingroup$ uh, and the leading stuff as well, of course ;-) $\endgroup$
    – Yves Klett
    Jun 4, 2014 at 15:20
  • $\begingroup$ Thanks a bunch for your comments and answer. Please let me check your propose to my goal. $\endgroup$ Jun 4, 2014 at 15:25

2 Answers 2

2
$\begingroup$

I like Yves Klett's method a lot, and if you are making the kind of substitutions he illustrates it is the way to go. However here is how to do just what you requested, as well as another alternative using "indexed objects."

Set @@ Append[ToHeldExpression @ FileBaseName @ #, Import @ #] & /@ filenames

(ToHeldExpression is a deprecated function but still entirely usable and handy.)

And now indexed objects with strings as keys:

(data[FileBaseName @ #] = Import[#]) & /@ filenames

This is used with e.g.:

data["Ronaldo"]
{2,3,5,8,0,1}
data /@ {"Beckenbauer", "Zeydane"}
{{11, 42, 5, 2, 7, 21}, {2, 8, 7, 13, 77}}
$\endgroup$
2
  • $\begingroup$ Blush :D Any particular reason not to use ToExpression? $\endgroup$
    – Yves Klett
    Jun 5, 2014 at 19:47
  • 1
    $\begingroup$ @Yves I presume you mean in place of ToHeldExpression etc.? Only that if those Symbols are already assigned, e.g. if you externally update the files then attempt to reload them, it will fail. $\endgroup$
    – Mr.Wizard
    Jun 6, 2014 at 1:19
3
$\begingroup$

Instead of generating hardcoded variables and cluttering your namespace, I would suggest you try this:

names = {"a.txt", "b.txt", "c.txt"};
vals = {1, 2, 3};
vars = ToExpression[FileBaseName /@ names];
data=Thread[vars -> vals]

{a -> 1, b -> 2, c -> 3}

This allows for comfortable further calculations together with ReplaceAll and similar and will empower your football algebra:

a*b*c/.data

6

Leaving the variable names in their string representation will work as well. More typing will be involved, but the process will be more robust with respect to name clashes (you will probably be safe with Beckenbauer though...)

names = {"a.txt", "b.txt", "c.txt"};
vals = {1, 2, 3};
vars = FileBaseName /@ names;
data = Thread[vars -> vals]
"a"*"b"*"c" /. data

{"a" -> 1, "b" -> 2, "c" -> 3}

6

$\endgroup$

Your Answer

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

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