1
$\begingroup$

I need to import about 100000 "dat" files to my progrom. I want to do this using the last answer to this question:

Importing multiple files using a for-loop

I know that I should define a list of all the names of my files, and at the end, I Import all of them. But I have a problem with this. The names of my files obey a special pattern, like this:

"mutomu,dms41,x1,dms51,x2,q24,x3,q25,x4.dat"

Which x1-x4 are integers in this way:

{x1, 1, 21}, {x2, x1, 21}, {x3, 1, 15}, {x4, 1, 15}

. When I am defining the names of my files, I do it as:

allprobabilityfiles = {Flatten[Table[{{x1, x2, x3, x4},"mutomu,dms41,x1,dms51,x2,q24,x3,q25,x4.dat"}, {x1, 1, 21,1}, {x2, x1, 21, 1}, {x3, 1, 15, 1}, {x4, 1, 15, 1}], 3]};

but in this way, the result is:

....{{21, 21, 6, 5}, "mutomu,dms41,x1,dms51,x2,q24,x3,q25,x4.dat"}, 
{{21, 21, 6, 6}, "mutomu,dms41,x1,dms51,x2,q24,x3,q25,x4.dat"}, 
{{21, 21, 6, 7}, "mutomu,dms41,x1,dms51,x2,q24,x3,q25,x4.dat"}, ....

(x1-x4 don't change inside the file names in this table), but what I need is the following:

....{{21, 21, 6, 5}, "mutomu,dms41,21,dms51,21,q24,6,q25,5.dat"}, 
{{21, 21, 6, 6}, "mutomu,dms41,21,dms51,21,q24,5,q25,6.dat"}, 
{{21, 21, 6, 7}, "mutomu,dms41,21,dms51,21,q24,5,q25,7.dat"}, ....

How can I solve this problem?

$\endgroup$
1
  • 1
    $\begingroup$ You can also refer to this solution :D $\endgroup$
    – xzczd
    Apr 8, 2013 at 5:39

3 Answers 3

2
$\begingroup$

StringForm is useful for embedding values into a control string:

filestring[vals__] := 
 ToString @ StringForm["mutomu,dms41,``,dms51,``,q24,``,q25,``.dat", vals]

filestring[10, 20, 30, 40]
(*  "mutomu,dms41,10,dms51,20,q24,30,q25,40.dat"  *)

You would just drop this function into your Table like so:

allprobabilityfiles = 
  Flatten[Table[{{x1, x2, x3, x4}, filestring[x1, x2, x3, x4]}, 
   {x1, 1, 21, 1}, {x2, x1, 21, 1}, {x3, 1, 15, 1}, {x4, 1, 15, 1}], 3];
$\endgroup$
1
  • $\begingroup$ Thanks for your answer. I liked it :)... $\endgroup$ Apr 8, 2013 at 10:40
1
$\begingroup$

You may be able to use FileNames to select the files you want to import.

data=Import/@FileNames["/mydirpath/mutomu*dat"];

Or it may prove easier to move the data files to their own directory where you can use:

data=Import/@FileNames["/mydatadirpath/*"];
$\endgroup$
3
  • $\begingroup$ Thanks for your answer. I knew this way, but I was avoiding to use it. I need to do it exactly as yulinlinyu's answer. $\endgroup$ Apr 8, 2013 at 9:28
  • $\begingroup$ I like this answer. $\endgroup$
    – yulinlinyu
    Apr 8, 2013 at 9:36
  • $\begingroup$ @Zahra Thank you, I'm glad you found the right solution to your problem :) $\endgroup$ Apr 8, 2013 at 12:50
1
$\begingroup$

You can do like this:

Table[{x1, "mutomu," <> ToString[x1] <> ",dms51.dat"}, {x1, 
  Range@4}]

(* {{1, "mutomu,1,dms51.dat"}, {2, "mutomu,2,dms51.dat"}, {3, "mutomu,3,dms51.dat"}, {4, "mutomu,4,dms51.dat"}} *)

It is very easy to implement in your cases.

Edit:

Ok, I'll fit the code above for your case:

allprobabilityfiles = 
  Flatten[Table[{{x1, x2, x3, x4}, 
     "mutomu,dms41," <> ToString[x1] <> ",dms51," <> ToString[x2] <> 
      ",q24," <> ToString[x3] <> ",q25," <> ToString[x4] <> 
      ".dat"}, {x1, 1, 21, 1}, {x2, 1, 21, 1}, {x3, 1, 15, 1}, {x4, 1,
      15, 1}], 3];
allprobabilityfiles[[1 ;; 5]]

(* {{{1, 1, 1, 1}, "mutomu,dms41,1,dms51,1,q24,1,q25,1.dat"}, {{1, 1, 1, 2}, "mutomu,dms41,1,dms51,1,q24,1,q25,2.dat"}, {{1, 1, 1, 3}, "mutomu,dms41,1,dms51,1,q24,1,q25,3.dat"}, {{1, 1, 1, 4}, "mutomu,dms41,1,dms51,1,q24,1,q25,4.dat"}, {{1, 1, 1, 5}, "mutomu,dms41,1,dms51,1,q24,1,q25,5.dat"}} *)

More elegantly:

fileName := {{##}, 
    "mutomu,dms41," <> ToString[#1] <> ",dms51," <> ToString[#2] <> 
     ",q24," <> ToString[#3] <> ",q25," <> ToString[#4] <> ".dat"} &;
Flatten[Array[fileName, {21, 21, 15, 15}], 3]
$\endgroup$
3
  • $\begingroup$ Thanks for your answer, but this isn't what I am looking for. "mutomu,dms41,21,dms51,21,q24,6,q25,5.dat" (for example) is the name of my file, and at the end I should have something like that! $\endgroup$ Apr 8, 2013 at 5:23
  • $\begingroup$ Thanks again. I just have one other question. I can see that <> must be available before and after ToString to put everything in the correct form. However, what is <> really doing? $\endgroup$ Apr 8, 2013 at 6:13
  • $\begingroup$ StringJoin. I think you can type ?<> in Mathematica to get the information. $\endgroup$
    – yulinlinyu
    Apr 8, 2013 at 9:35

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.