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.

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?

share|improve this question
    
How do you check if y==4 without importing the files somehow ? –  b.gatessucks Jan 8 '13 at 10:48
    
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. –  su1 Jan 8 '13 at 10:58
    
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. –  Ernst Stelzer Jan 8 '13 at 14:46
    
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. –  george2079 Jan 8 '13 at 16:46

1 Answer 1

up vote 5 down vote accepted

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

Select[Select[FileNames["file*.txt"],Length[FindList[#,{"y","4"},2]]===2&],
StringTake[FindList[#,{"y","4"},2][[2]],{3}]==="4"&]
share|improve this answer

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.