2
$\begingroup$

I have a very big data set that I have generated in Matlab. I am importing into Mathematica since doing some data manipulation is much easier.

There is one caveat, which is the following: I have some data entries that appear as the string

"0.677 + 9.121e-12i"

or

"1.247e-12 + 0.182i"

where e-12 is actually the exponential of -12 (i.e. very small). Due to the formatting of the exponential, Mathematica does not recognise it as an exponential. Hence, I want to set any term that contains e to be 0 such that for the two entries above, I get:

"0.677"
"0.182i"

I am then able to use ToExpression to recast the whole data set to something that I can further manipulate.

$\endgroup$
3
$\begingroup$

You can use the following operator:

op = ToExpression@*StringReplace[{"e+" -> "*^", "e-" -> "*^-", "i" -> "I"}]

which can then be applied to strings of numbers:

op["0.677 + 9.121e-12i"]
op["1.247e-12 + 0.182i"]
$\endgroup$
  • $\begingroup$ Can this be applied to a matrix of strings? $\endgroup$ – Sid Nov 15 '19 at 16:53
  • $\begingroup$ i.e. currently, I have to apply it recursively on each element of the matrix. Any better way to do this? $\endgroup$ – Sid Nov 15 '19 at 16:55
  • 2
    $\begingroup$ @Sid, try Map[op, mat, {2}] $\endgroup$ – J. M. is in limbo Nov 15 '19 at 17:00
  • $\begingroup$ I would throw a Chop in op to get rid of the small terms and produce the wanted results. $\endgroup$ – That Gravity Guy Nov 15 '19 at 22:46

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.