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.

I have a txt file with 3 columns of data:

x y1 y2

How do I import the file then create 2 functions, one interpolated with {x,y1} values and the other interpolated with {x,y2} values?

This is the sort of thing I want to achieve (in pseudocode):

Import "data.txt" into A
f1 = Interpolation(A,{x,y1})
f2 = Interpolation(A,{x,y2}) 
share|improve this question
    
Did you happen to look up the Import documentation? This and its subpage on basic formats (search for "Table") would be a good starter.Interpolation also has a very readable doc page. To get the table you obtain after importing into separate columns, you need Transpose. Please, try to reach a minimum proficiency level of the language by reading the documentation before asking questions. –  Sjoerd C. de Vries Jun 22 '12 at 12:39
1  
For taking parts of arrays you could check out the documentation of Part –  Heike Jun 22 '12 at 12:45
add comment

1 Answer 1

up vote 6 down vote accepted

Maybe something like

a = Transpose[Import["data.txt", "Table"]];

f1 = Interpolation[{a[[1]], a[[2]]} // Transpose];
f2 = Interpolation[{a[[1]], a[[3]]} // Transpose];

might work?

Alternatively,

a = Import["data.txt", "Table"];

f1 = Interpolation[Drop[#, {3}] & /@ a];
f2 = Interpolation[Drop[#, {2}] & /@ a];

or even

f1 = Interpolation[ a[[All, {1, 2}]] ];
f2 = Interpolation[ a[[All, {1, 3}]] ];
share|improve this answer
    
For the second one, you can also use a[[All,{1,2}]] and a[[All, {1,3}]] instead of Drop[...]&/@a. –  Heike Jun 22 '12 at 12:42
    
Thanks, but only the first method worked for me –  Eddy Jun 22 '12 at 13:25
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.