1
$\begingroup$

I have a text file where each line is a data point in the form:

[ -495.01172, -158.35966, 2705.0 ]
[ -489.15576, -127.229675, 2673.0 ]
[ -487.6918, -97.679855, 2665.0 ]
[ -487.32578, -68.4594, 2663.0 ]
[ -485.86182, -39.19415, 2655.0 ]
[ -485.3128, -10.12311, 2652.0 ]
[ -484.03183, 18.853745, 2645.0 ]
[ -482.75082, 47.677364, 2638.0 ]
[ -481.6528, 76.37677, 2632.0 ]
[ -481.6528, 105.184616, 2632.0 ]
...

Each line represents [x,y,z]. I need to 3D plot these, but I am getting errors. Below is what I've tried along with the resulting error.

data = Import[
  "C:\\Users\\user\\Desktop\\out.txt", "text"]

data2 = List[
  StringReplace[
   data, {"[" -> "{", "]" -> "}", "\n" -> ",", " " -> ""}]]

ListPointPlot3D[data2]

The first two lines run successfully. The last line returns:

...{1161.5232,-887.44867,1677.0}} must be a valid array or a list of valid arrays >>
$\endgroup$
11
  • $\begingroup$ "I have a text file..." - post it on Pastebin, please. $\endgroup$ Commented Nov 8, 2017 at 19:14
  • $\begingroup$ @J.M. Why? Is the sample I've provided not enough? Or are you wanting to see it in a specific format? The format I've shown above is the same for every row. $\endgroup$ Commented Nov 8, 2017 at 19:16
  • $\begingroup$ Either way, Pastebin says I can't paste that much data. $\endgroup$ Commented Nov 8, 2017 at 19:16
  • $\begingroup$ I suspect that your problem is in how you import your file, and possibly something wonky within your file. $\endgroup$ Commented Nov 8, 2017 at 19:17
  • 1
    $\begingroup$ ToExpression should be used somewhere to convert the string in a mathematica expression $\endgroup$
    – andre314
    Commented Nov 8, 2017 at 19:25

3 Answers 3

4
$\begingroup$

This works, but may be there is easier way

SetDirectory[NotebookDirectory[]]
data  = Flatten@Import["input.txt","CSV"];
data2 = If[StringQ[#],ToExpression[StringDelete[#,{"[","]"}]],#]&/@data;
data2 = ArrayReshape[data2,{Length@data2/3,3}]

now

MatrixForm[data2]

Mathematica graphics

Plot it

ListPointPlot3D[data2]

Mathematica graphics

The input file is what was shown in OP

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ This worked fine, thanks $\endgroup$ Commented Nov 8, 2017 at 21:01
2
$\begingroup$

I copied the 10 lines in your question and pasted them into a text file called out.txt.

data = Import["D:\\at_work\\mathematica\\stack_exchange\out.txt", 
  "text"]

The basic problem with the way you were working is that what appears to be numbers are strings and you have to convert them.

I did that as follows. First I split them into separate lines.

dataN = StringSplit[data, EndOfLine]

This produces a list of strings of the form you indicated for each line, one per line.

Now I used StringCases on each of these lines converting the strings to numbers.

data2 = Map[
  StringCases[#, 
    "[" ~~ Whitespace ~~ x__ ~~ "," ~~ Whitespace ~~ y__ ~~ "," ~~ 
      Whitespace ~~ z__ ~~ Whitespace ~~ "]" :> 
     Sequence[ToExpression[x], ToExpression[y], ToExpression[z]]] &,
  dataN
  ]

(* {{-495.012, -158.36, 2705.}, {-489.156, -127.23, 
  2673.}, {-487.692, -97.6799, 2665.}, {-487.326, -68.4594, 
  2663.}, {-485.862, -39.1942, 2655.}, {-485.313, -10.1231, 
  2652.}, {-484.032, 18.8537, 2645.}, {-482.751, 47.6774, 
  2638.}, {-481.653, 76.3768, 2632.}, {-481.653, 105.185, 2632.}} *)

The output is a list of lists with three real numbers in each sub-list.

ListPlot3D[data2]

Mathematica graphics

$\endgroup$
2
$\begingroup$

another approach.

ImportString[
 StringReplace[
  Import["test.txt", "Text"], {"[", "]"} -> ""], "CSV"] 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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