2
$\begingroup$

I export a simple CSV file created in MATLAB with complex numbers, such as:

0.1+2.1i,0.9+0.13i,0.02+0.3i

I would like to import it into Mathematica with the "i" recognized as the imaginary unit, of course. Unfortunately, a simple Import["filename"] command does not work, and neither does Import["filename","CSV"]. I have also tried the solution method proposed here, but unfortunately without success.

How can I do it?

PS: Following Bill's suggestion, here is the output from InputForm:

{{"0.1+2.1i", "0.9+0.13i", "0.02+0.3i"}}
$\endgroup$
1
  • 2
    $\begingroup$ Please edit your post to include the result of InputForm[Import["filename"]] and put that on a single line by itself with four spaces in front of it so Stackexchange will understand that line is code and to do less "desktop publishing" on it before we get to see it. That will hopefully show people here exactly what Mathematica is getting from your file import and then people can try writing a line of code to fix it for you $\endgroup$
    – Bill
    Jun 2 at 14:07

2 Answers 2

5
$\begingroup$

How about (after reading the input), then for each entry in your list of lists do

 lis = {"0.1+2.1i", "0.9+0.13i", "0.02+0.3i"};
 new = StringReplace[#, "i" -> "*I"] & /@ lis
 new = ToExpression[new]

Mathematica graphics

You can apply the above to your input, depending on what you used to import the Matlab file.


As an appendix (not official answer), if you have access to the new Wolfram language LLMFunction you can also try

lis={"0.1+2.1i","0.9+0.13i","0.02+0.3i"};
LLMFunction["please convert these from Matlab to Mathematica ``. Thank you."][lis];
ToExpression[%]

 (* {0.1 + 2.1 I, 0.9 + 0.13 I, 0.02 + 0.3 I} *)

Screen shot on V 13.2.1

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thanks! That worked! $\endgroup$
    – Philipp
    Jun 2 at 15:18
2
$\begingroup$

Alternatively,

lis = {"0.1+2.1i", "0.9+0.13i", "0.02+0.3i"};

SemanticInterpretation /@ lis

(* {0.1 + 2.1 I, 0.9 + 0.13 I, 0.02 + 0.3 I} *)
$\endgroup$

Your Answer

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

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