0
$\begingroup$

I have several data files I want to import. The names of these files consist of a part that is the same for all of them and a number which identifies them, as in "restofname-NUMBER". I tried to make a list list of the numbers in question and then to import by

Import["restofname-" <> # & /@ StringForm[list], "Table"],

but that leaves me with the error

String expected at position 1 in StringForm[1]

I'm guessing StringForm is not what I need, but have no idea what else to use.

$\endgroup$
5
  • 1
    $\begingroup$ try ToString . $\endgroup$
    – george2079
    Apr 10, 2015 at 11:39
  • $\begingroup$ Argh, what a mess of brackets I made. Deleted the previous comment. This seems to work properly: (data[#] = Import["restofname-" <> ToString[#], "Table"]) & /@ list $\endgroup$
    – LLlAMnYP
    Apr 10, 2015 at 12:03
  • 1
    $\begingroup$ Also check IntegerString[] in case you need padding, e.g. name-001.dat $\endgroup$ Apr 10, 2015 at 12:58
  • $\begingroup$ Concerning "what else to use": ?*String* can be worth a try. $\endgroup$ Apr 10, 2015 at 14:27
  • $\begingroup$ Possible duplicate of mathematica.stackexchange.com/questions/61652/… $\endgroup$ Apr 13, 2015 at 13:02

1 Answer 1

5
$\begingroup$

I am not sure if this is the best way of doing it, so the following is not exactly an answer to the question. Here is how I would do it assuming that I have padding.

0 Preparation

data={1,2,3};(*some test data*)
(*exporting to some files*)
Table[Export["myData" <> IntegerString[i, 10, 5] <> ".txt", data], {i,10}];
(*and deleting one to make it a bit more complicated*)
DeleteFile["myData00003.txt"];

Now how to proceed? Check the files.

FileNames[] 

{"desktop.ini","Ma musique", "Mes images","Mes vidéos","myData00001.txt", "myData00002.txt", "myData00004.txt", "myData00005.txt", "myData00006.txt", "myData00007.txt", "myData00008.txt", "myData00009.txt", "myData00010.txt", "test.xls"}

(*Now let us take only the ones that start with the right name*)
fList = Select[FileNames[], (StringMatchQ[#, StartOfString ~~ "myData" ~~ __] &)];
(*or directly using the power of FileNames[]*)
fList = FileNames["myData*"]

{"myData00001.txt", "myData00002.txt", "myData00004.txt", \ "myData00005.txt", "myData00006.txt", "myData00007.txt", \ "myData00008.txt", "myData00009.txt", "myData00010.txt"}

(*If we want the numbers of those we can use regular expressions*)
(*If the lengths are all fixed, StringDrop[] can also work*)
numbers=ToExpression[Flatten[StringCases[#, RegularExpression["[0-9]+"]] & /@ fList]]

{1, 2, 4, 5, 6, 7, 8, 9, 10}

So there are two ways now:

1a Knowing the numbers

If we know that it starts at 1 and ends at 10 with 3 missing, we just do:

Table[If[i != 3, iData[i] = Import["myData" <> IntegerString[i, 10, 5]<> ".txt","Table"]], {i, 10}];

1b Using the file list

If we are not sure about the numbers we use the file list and extract the numbers from the file names:

Table[ 
    newiData[   
        ToExpression[     
            StringCases[i, RegularExpression["[0-9]+"] 
            ]
        ][[1]]
    ] = Import[i, "Table"], {i, fList}]

Remarks

Now iData[i] and newiData[i] should be identical.

Note that especially the second case works if you have file names of the type:

"known name"<>"variational unknown part"<>"unknown length numbers"<>".dat" 
$\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.