2
$\begingroup$

I'm trying to import many files which have the same name "x.data", but they end with a extension "x_code1.data".

I was trying to import all files with the routine:

Importdata1 = 
Import["x_code1.dat", "Table"];

But I want to have an array with all the codes, something similar to:

For i = {1,4,7,10}

Importalldata[i] = 
Import["x_code[i].dat", "Table"];

How can I achieve this?

Thank you so much

$\endgroup$
  • $\begingroup$ Import[#, "Table"] & /@ Table["xcode" <> IntegerString[i, 10] <> ".data", {i, 1, 10}] $\endgroup$ – user1066 Jul 1 '18 at 12:37
3
$\begingroup$

You can create the list of filenames using StringTemplate

Table[
 StringTemplate["FileNameBase``.extension"][k]
 , {k, 5}
 ]
(* {"FileNameBase1.extension", "FileNameBase2.extension", \
"FileNameBase3.extension", "FileNameBase4.extension", \
"FileNameBase5.extension"} *)

Now we apply this to your case

alldata = Table[
  Import[
   StringTemplate["x_code``.dat"][k]
   , "Table"]
  , {k, {1,4,7,10}}
  ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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