Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
temp = (layer = #;
    layerS = ToString[#];
    StringForm["Import[\"drainCdg_gdat0``\",\"Table\"]", 
     layerS]) & /@ {1, 2, 3}

how to get the output of this to actually execute Import command? i.e. go to a directory and import a series of files with indexed filenames ?

share|improve this question
1  
You've seen ToExpression[]? –  J. M. Jun 3 '13 at 16:12
    
I'm not sure if you want to use StringForm if you want to turn these strings into expressions. StringForm only changes the display. See StringJoin. –  Jacob Akkerboom Jun 3 '13 at 16:36
1  
@JacobAkkerboom Agreed. Also, if one would insist in using StringForm, the preceding ToString would be fully unnecessary. StringForm doesn't need strings for its slots. –  Sjoerd C. de Vries Jun 3 '13 at 21:20
    
@SjoerdC.deVries I wonder if there is a convenient way at all to work with StringForm. Ah I see I need to edit my answer a bit. –  Jacob Akkerboom Jun 3 '13 at 21:31
1  
@JacobAkkerboom You can throw ToString on StringForm. This will turn it into a proper string. –  Sjoerd C. de Vries Jun 3 '13 at 21:39
show 1 more comment

2 Answers

How about

heldImports = 
 Import["drainCdg_gdat0" <> ToString[#], "Table"] & /@ Hold[1, 2, 3]

Hold[(Import["drainCdg_gdat0" <> ToString[#1], "Table"] &)[1],
(Import["drainCdg_gdat0" <> ToString[#1], "Table"] &)[2],
(Import["drainCdg_gdat0" <> ToString[#1], "Table"] &)[3]]

and then

thisAndThatTable = heldImports[[1]]

Or, maybe more conventionally

heldImports = 
 Import @@@ (Hold @@ ({"drainCdg_gdat0" <> ToString[#], 
        "Table"} & /@ {1, 2, 3}))

Hold[Import["drainCdg_gdat01", "Table"],
Import["drainCdg_gdat02", "Table"],
Import["drainCdg_gdat03", "Table"]]

Note that <> stands for StringJoin.

You could have done something similar with ToExpression. Just don't use StringForm. To see that StringForm is bad for this purpose, note that

StringForm["Import[\"drainCdg_gdat0``\",\"Table\"]", 1] // FullForm

StringForm["Import[\"drainCdg_gdat0``\",\"Table\"]",1]

The following is an example using ToExpression

importStrings = 
  "Import[\"drainCdg_gdat0" <> ToString[#] <> "\",\"Table\"]" & /@ {1,
     2, 3}

{"Import[\"drainCdg_gdat01\",\"Table\"]",
"Import[\"drainCdg_gdat02\",\"Table\"]",
"Import[\"drainCdg_gdat03\", \"Table\"]"}

import[i_] := ToExpression[importStrings[[i]]];

suchAndSuchTable = import[2];

You can use SetDirectory["dir"] to go to the relevant directory. Or you can prepend the path to the filename using StringJoin (<>), or you can just write "/dir1/dir2/drainCdg_gdat02" of course.

share|improve this answer
add comment

If what you are trying to do is "go to a directory and import a series of files with indexed filenames" there is a simpler way. Say you want to import all the .tif files in a directory with address path.

fileNames = FileNames["*.tif", path];
allFiles = Table[Import[fileNames[[num]]], {num, 1, Length[fileNames]}];

This imports them all at once and they can be indexed as allFiles[[1]], allFiles[[2]], etc. More generally, you can build a list of all the file names and then import the whole list. More succinctly, you can write

allFiles = Import /@ fileNames

to get the same result.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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