1
$\begingroup$

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?

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

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}}*)
$\endgroup$
  • $\begingroup$ As an alternative Part notation, [[All, 2;;]] is a bit syntactic sugar, but might be easier to read/understand quickly. $\endgroup$ – Thies Heidecke Jan 5 '17 at 12:03
  • $\begingroup$ @Marchi, if i don't want to read the last column, how should i proced? $\endgroup$ – Diogo Jan 8 '17 at 18:35
3
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

To do it differently:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.