Let's say I have three files containing:

File 1:
x y z
1 4 6 

File 2:
x y z
2 5 9 

File 3:
x y z
5 4 8

My goal is to import the data from a file only if in this file y == 4 (so here I would want to import files 1 and 3).

Is there a way to do that with functional programming? The only solution I can think of currently is to loop through all the files, check if data[[2, 2]] == 4, and append this data to a list if true. Is there a better way to do that?

And if I have to loop, is it better to import all the files as lists and then discard the lists that do not match y == 4? Or is it better to import only the files matching y == 4 in the first place?

  • $\begingroup$ How do you check if y==4 without importing the files somehow ? $\endgroup$ Commented Jan 8, 2013 at 10:48
  • $\begingroup$ ok "importing" maybe was not the right word. Let's say I want to keep those files only, and I'm wondering if there is a function with an option allowing me to do something like Import[file, only if y == 4], and if not, if from a list with the data from all files, I can know without looping what files contain y == 4. $\endgroup$
    – Sulli
    Commented Jan 8, 2013 at 10:58
  • $\begingroup$ In Mathematica 9 ImageFileApply and ImageFileScan "can be used to read, process, and write successive blocks of data from a very large image file whose size could exceed available memory", i.e. to process without excessive loading. $\endgroup$ Commented Jan 8, 2013 at 14:46
  • $\begingroup$ If its a large file with tag info at the beginning you might want the do raw open/read operations to get the key you need, then import only as needed. For small files my guess is you are just as well off to import the whole thing and descard what yuo dont need. $\endgroup$
    – george2079
    Commented Jan 8, 2013 at 16:46

1 Answer 1


If your data is really that simple, then you can use FileNames and Select, e.g.:


Your Answer

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

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