0
$\begingroup$

I have a code that is generating some numerical data in the form of a list of trajectories. Each trajectory itself is a list of 2D coordinates. Each coord. is a list of 2 numbers.(see example data below)

The code can write this data to a file adding adequate braces and commas so that if the output was to be copy-pasted into a mathematica notebook, it would correctly parse as a list of list of list, ignoring white-space.

The problem is that copy-pasting isn't convenient for larger file sizes. So, is there a format in which i should export my data so that a corresponding import command would recognize it as a list of list of list?What would be the corresponding import command?

So far I have realized that tab separated format with Import[filePath,"Table"] will work for 2D arrays. On the other hand,"list like output format" when directly imported doesn't parse properly. I did found a question on se regarding importing 3D lists but they had been exported from Matlab first, which I am not using.

To be clear, I can output the numbers in any way I want-with/without braces, multiline, single line, comma/tab separated etc. I just need to know a format that would be properly read as a 3D list.

Here's an example output
(ignoring whitespace this is of the form{{a,b},{c},{d,...}...})

{Nothing
,
{{0.5,0.0}
}
,
{{0.51,0.031415926535897934}
,{0.4990133642141358,0.03139525976465669}
}
,
{{0.52,0.06283185307179587}
,{0.5070210091497347,0.06337709934111496}
}
,
{{0.53,0.0942477796076938}
,{0.5150286540853336,0.09535893891757323}
}
,
{{0.54,0.12566370614359174}
,{0.5230362990209325,0.1273407784940315}
}
,
{{0.55,0.15707963267948966}
,{0.5310439439565314,0.15932261807048975}
}
,
{{0.56,0.18849555921538758}
,{0.5390515888921303,0.19130445764694803}
}
,
{{0.5700000000000001,0.2199114857512855}
,{0.5470592338277293,0.22328629722340632}
}
,
{{0.5800000000000001,0.2513274122871834}
,{0.5550668787633282,0.2552681367998646}
}
,
{{0.5900000000000001,0.28274333882308134}
,{0.5630745236989271,0.2872499763763229}
}
}
$\endgroup$
  • $\begingroup$ Have you tried Import[filePath, "List"]? $\endgroup$ – Rohit Namjoshi Oct 22 at 0:21
  • $\begingroup$ @RohitNamjoshi doesn't parse properly $\endgroup$ – lineage Oct 22 at 0:28
  • $\begingroup$ It is not clear exactly how the file you are trying to import is formatted. If it is exactly like what you posted, then importing as "List" should parse correctly and return the same list. Can you provide a link to Pastebin or Dropbox or gDrive with a sample file? $\endgroup$ – Rohit Namjoshi Oct 22 at 0:45
  • $\begingroup$ yeah the data is a complete example..doesn't parse with list $\endgroup$ – lineage Oct 22 at 0:51
  • $\begingroup$ Try list = copy/paste from your post. text = ExportString[list, "Text"]; listImport = ImportString[text, "List"]. Apart from Nothing, list and listImport are identical. $\endgroup$ – Rohit Namjoshi Oct 22 at 0:58
1
$\begingroup$

One thing that works
For the following data

{
{{x11,y11},{x12,y12},..},
{{x21,y21},{x22,y22},..},
...
}

export in a .txt as following

x11 y11 x12 y12 x13 y13...x1n y1n 
x21 y21 x22 y22 ...x2 y2m
...

Read using ReadList[filePath, {Number, Number}, RecordLists -> True]

  • Readlist makes the outermost list
  • RecordLists -> True helps make middle list (every new line)
  • {Number,Number} makes the innermost list (the coords)
$\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.