4
$\begingroup$

I have data such as,

 {{[Part(33.0, 34.0), Part(33.0, 34.0), Part(31.0, 32.0), Part(30.0, 32.0), Part(29.0, 32.0), Part(28.0, 29.0), Part(28.0, 29.0)]}}

as text data.

I want to get number list of

 {{33.0, 34.0},{33.0, 34.0}, {31.0, 32.0}, {30.0, 32.0}, {29.0, 32.0}, {28.0, 29.0},{28.0, 29.0}} then make it density plot or Histogram plot.

I tried to convert it as follows,

td0 = FileNames["myfolder/data.*txt"];
td0data = Import[#, "Data"] & /@ td0;

( The out put is like

{{[Part(33.0, 34.0), Part(33.0, 34.0), Part(31.0, 32.0), Part(30.0, 32.0), Part(29.0, 32.0), Part(28.0, 29.0), Part(28.0, 29.0)]}}

) Hence, I did

ss = StringSplit[td0data[[1]], {", Part"}];
sr = StringReplace[ss[[1]], {"(" -> "{", ")" -> "}"}];
sdfirst = StringDrop[sr[[1]], 5];
sdlast = StringDrop[sr[[-1]], -1];

sj = StringJoin[sdfirst, sr[[2 ;; -2]], sdlast];
sr = StringReplace[sj, "}" -> "},"];
srd = StringDrop[sr, -1];

( Here the output was like

{33.0, 34.0},{33.0, 34.0}, {31.0, 32.0}, {30.0, 32.0}, {29.0, 32.0}, {28.0, 29.0},{28.0, 29.0}

) )

So at last I wished I converted this String to Number, as

te = {ToExpression["srd"]};

The output was

{{33.0, 34.0},{33.0, 34.0}, {31.0, 32.0}, {30.0, 32.0}, {29.0, 32.0}, {28.0, 29.0},{28.0, 29.0}}

However, te was Null and I cannot even do ListPlot. I don't know why it doesn't work.

If I just input the line by copy and paste,

test= {{33.0, 34.0},{33.0, 34.0}, {31.0, 32.0}, {30.0, 32.0}, {29.0, 32.0}, {28.0, 29.0},{28.0, 29.0}};

I can write Histogram

Histogram3D[N[test], {{-100, 100, 1}, {-100, 100, 1}}, ChartElementFunction -> "GradientScaleCube"]

(Even here, I have a problem that I cannot set x, y plot range)

Although I can copy and paste by hand for a single file, it is hard to make many files by hand...

I have two questions:

  1. How to make "te" to number list?
  2. I want to make Histogram3D plot, but Could you tell me how to set the histogram plot range? Each histogram bar often become rectangle...
$\endgroup$

4 Answers 4

4
$\begingroup$

Let's say your data is stored in a file that can be imported:

data = Import["C:/test.txt"]

test = First /@ 
  StringCases[data, 
   "(" ~~ a : Except[")"] .. ~~ ")" :> 
    ToExpression /@ List @@ {StringSplit[a, ","]}]

{{33., 34.}, {33., 34.}, {31., 32.}, {30., 32.}, {29., 32.}, {28., 29.}, {28., 29.}}

To get ranges:

r1 = Floor@({-1, 1} + MinMax[test[[All, 1]]])~Join~{1};
r2 = Floor@({-1, 1} + MinMax[test[[All, 2]]])~Join~{1};
Histogram3D[test, {r1, r2}, 
 ChartElementFunction -> "GradientScaleCube"]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you so much!! I could successfully get the number data, and also the 3D histogram. I really appreciate the kind instruction. $\endgroup$
    – rani
    Commented Dec 21, 2023 at 16:21
  • $\begingroup$ You are most welcome. Thank you for the accept. $\endgroup$
    – Syed
    Commented Dec 21, 2023 at 16:26
  • $\begingroup$ It was very nice of you. Thanks again! $\endgroup$
    – rani
    Commented Dec 21, 2023 at 16:30
4
$\begingroup$

What followed "Hence, I did" produced several error messages for me, so I don't know how you got anything to work. There's always a problem when we're not given the exact data. If you start by importing a file, then you need to give us the file. If only the string matters, then don't bother showing us how you imported the file contents and just give us the exact string. Anyway, I assume we're starting with a string like this:

stringData = "{{[Part(33.0, 34.0), Part(33.0, 34.0), Part(31.0, 32.0), Part(30.0, 32.0), Part(29.0, 32.0), Part(28.0, 29.0), Part(28.0, 29.0)]}}"

From there you can use StringCases. Syed already showed you one way, but here's one with regular expressions:

result = StringCases[stringData, RegularExpression["\\(([^)]*)\\)"] :> ToExpression["{$1}"]]
(* {{33.,34.},{33.,34.},{31.,32.},{30.,32.},{29.,32.},{28.,29.},{28.,29.}} *)
$\endgroup$
1
  • $\begingroup$ Thank you VERY much! I succeeded in "To Expression". (Until then, I had error message.) I'm so sorry for not knowing how to share the text file on this site... Well, as you told me, since I couldn't upload the file, I actually struggled to make the same form as stringData. In my original data, there was no double quotation mark. However, by executing ToSTring[td0data], I got same expresssion as stringData. Then I could get same ansers. Thanks again! $\endgroup$
    – rani
    Commented Dec 21, 2023 at 16:29
4
$\begingroup$

Using StringDelete and StringReplace:

sdata = "{{[Part(33.0, 34.0), Part(33.0, 34.0), Part(31.0, 32.0), Part(30.0, 32.0), Part(29.0, 32.0), Part(28.0, 29.0), Part(28.0, 29.0)]}}";

patt = {LetterCharacter, "[", "]"};
rep = {"(" -> "{", ")" -> "}"};

First@ToExpression@StringReplace[StringDelete[#, Alternatives @@ patt], rep] &@sdata

(*{{33., 34.}, {33., 34.}, {31., 32.}, {30., 32.}, {29., 32.}, {28., 29.}, {28., 29.}}*)
$\endgroup$
1
  • 1
    $\begingroup$ Thank you very much for your help! I could get same answer as you told me. All of your comment was very helpful, but it seems I can only put "accept" for only one person... so I put it to the other person who responded me first. I'm sorry for that. But I sincerely thank you, too. $\endgroup$
    – rani
    Commented Dec 21, 2023 at 16:34
2
$\begingroup$

A hash-free variant of E. Chan-López answer (no need to use Alternatives)

First @ ToExpression @ StringReplace[{"(" -> "{", ")" -> "}"}] @
   StringDelete[{LetterCharacter, "[", "]"}] @ sdata

{{33., 34.}, {33., 34.}, {31., 32.}, {30., 32.}, {29., 32.}, {28., 29.}, {28., 29.}}

$\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.