2
$\begingroup$

I have a .txt file written as

1 2

3 4

5 6

7 8

9 10

11 12

I would like to import it as three matrices according to the rows.

I used b = Import["d:\am.txt", {"Table", Range[1, 2]}]

Seems it does not work, "Data" will be normal terminated, but not in the form of matrix.. Is there any solution for importing data in the format of table by specifying rows?

$\endgroup$
1
  • 1
    $\begingroup$ A suggestion: look at Skip and Read. Read as strings and convert with ImportString or related functions. $\endgroup$
    – Mr.Wizard
    Jan 24, 2015 at 7:41

2 Answers 2

3
$\begingroup$

Just rename "Table" to "Data"

Import["am.txt", {"Data", {1, 2}}]
(* {{1, 2}, {3, 4}} *)

P.S. delete empty lines in the text file if you have them

$\endgroup$
1
  • $\begingroup$ Thank you very much! It works now. $\endgroup$
    – user26143
    Jan 25, 2015 at 10:07
0
$\begingroup$

Assuming the data is stored in "matrix.txt", you can do the following:

m = Import["matrix.txt"];

temp = Partition[StringSplit[m], 4];

and finally form the matrices:

Partition[#, {2}] & /@ temp

This does the structure, maybe some type conversations are still necessary.

$\endgroup$

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.