# Importing .txt data into a matrix

I have the following .txt data:

 1               0               4               0
2               0             3.5               0
3             0.5               4               0
4               1               4               0


I would like to import this data and store it in the following format:

A={{0,4,0},{0,3.5,0},{0.5,4,0},{1,4,0}}


How can I do that?

• If i don't want to read the last column, how should i proced? – Diogo Jan 8 '17 at 18:39

It looks like you don't want the first column. Using Import:

data = Import["path/to/file/data.txt", "Table"][[;; , 2 ;;]]

(*{{0, 4, 0}, {0, 3.5, 0}, {0.5, 4, 0}, {1, 4, 0}}*)

• As an alternative Part notation, [[All, 2;;]] is a bit syntactic sugar, but might be easier to read/understand quickly. – Thies Heidecke Jan 5 '17 at 12:03
• @Marchi, if i don't want to read the last column, how should i proced? – Diogo Jan 8 '17 at 18:35

You can also use ReadList in a clever way for this, and since ReadList is a low level function, I always use it instead of Import especially for large files. Let's define what to omit from the result:

skip[_String] := Nothing (* use Sequence[] pre V. 10.2 *)
omit = skip[Word];


Now we can use ReadList directly:

ReadList["file.txt", {omit, Number, Number, Number}]

(* {{0, 4, 0}, {0, 3.5, 0}, {0.5, 4, 0}, {1, 4, 0}} *)


Or you can use it normally and do post-processing as the other answers show.

To do it differently:

A = Transpose @ Drop[Transpose @ Import["name_of_file.txt", "Table"], 1]