4
$\begingroup$

I have a $4 \times N$ matrix in a .txt file of the form:

-17.071 137.567 16.554 0.65

-16.873 136.138 16.934 0.7

-15.397 135.738 16.894 0.7

-14.7 135.982 15.9 0.6

... ($N-4$ additional row entries)

Where the numeric values in each row are separated by a space. How would I read this matrix in, and parse it in the following manner:

{ 
  {-17.071, 137.567, 16.554, 0.65},
  {-16.873, 136.138, 16.934, 0.7},
  {-15.397, 135.738, 16.894, 0.7},
  {-14.7, 135.982, 15.9, 0.6},
  ...
}

Evaluating

Import[PATH\\MATRIX.txt]] 

Only returns the printed contents of the file.

$\endgroup$
4
  • $\begingroup$ Please upload the file somewhere so that we can help you better. $\endgroup$
    – s0rce
    Mar 6, 2013 at 22:38
  • 1
    $\begingroup$ Related? $\endgroup$
    – cormullion
    Mar 6, 2013 at 22:41
  • $\begingroup$ i have a same question my file path is D:\ and my file name is f.txt so how can i import in mathematica? $\endgroup$ Mar 6, 2017 at 13:05
  • 1
    $\begingroup$ @HafizFahad did you see the accepted answer here? If Import[ filename , "Table" ] doesn't work I'd guess your file is structured differently. $\endgroup$
    – george2079
    Mar 6, 2017 at 15:48

1 Answer 1

11
$\begingroup$

I suspect that

data = Import[PATH\\MATRIX.txt, "Table"]

could work for you, however, I'm not sure without a copy of your file. If you post that I will try to update this answer.

$\endgroup$
2
  • $\begingroup$ Works! ReadList[PATH\\MATRIX.txt, {Number, Number, Number, Number}] also worked. $\endgroup$ Mar 6, 2013 at 22:40
  • 1
    $\begingroup$ You need a double quota for PATH\\MATRIX.txt $\endgroup$
    – James LT
    Jul 6, 2016 at 19:31

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.