I'm doing some calculations on complex numbers in Mathematica. I can use Export to generate a file (*.CSV or *.dat, or anything else) of complex numbers. If I want to use excel, origin or Matlab to process the data, they cann't be treated properly.

The easiest way to show these problem is like this:

   Export["dtList.dat", dtList = E^(-I*#) & /@ Range[10] // N]

When the file,dtList.dat, is imported to Matlab, the data are treated as string, not complex numbers, (Look at the figure attached).

I know this problem can be addressed if I export the Real part and Imaginary part of the complex numbers seperately, and combine them together in Matlab after import, but this is inconvenient and not elegant.

Is there any clever solution? Thank you.

enter image description here

  • $\begingroup$ If you want to use the lists in Excel or Matlab, why don't you export to these formats then? Opening something like Export["tmp/test.xls", E^(-I*#) & /@ Range[10] // N] in Excel looks fine for me. $\endgroup$ – halirutan Nov 7 '13 at 4:10
  • $\begingroup$ Excel does not directly support complex numbers, without any addons. For MATLAB I recommend MATLink (which halirutan has already mentioned). I am one of the authors of MATLink so please ask me if you are having trouble with it. $\endgroup$ – Szabolcs Nov 7 '13 at 18:04

For Matlab the solution is very simple

data = RandomComplex[1 + I, 100];

Export["test.mat", data]

For Excel the situation could be as easy but when you try it

Export["test.xls", data]

you will see that Excel does not recognize something like 1.0+2.0*I as complex number. It expects something like 1.0+2.0i instead. Therefore, one quick solution is

Export["test.xls", ToString[Re[#]]<>"+"<>ToString[Im[#]]<>"i"& /@ data]


I cannot test it in Matlab, but regarding your comment

No, it doesn't work, because the number is in the form of 0.540302 + 0.841471 I, which IS interpreted as a complex number in Mathematica, but NOT in Matlab.

it seems the normal Export will not work. Honestly, I find this very strange. We are now in version 9 of Mathematica and one could believe that someone has tried to export complex numbers to Matlab until now :-(

You could try the same approach as I gave for Excel. Or maybe you want to use MATLink to transfer your complex lists. If it is not working there, you have at least someone who you can ask for a quick fix.

  • $\begingroup$ Yes, MATLink does support transferring complex numbers to/from MATLAB :-) $\endgroup$ – Szabolcs Nov 7 '13 at 18:04

I don't have MatLab, so you'll have to verify for me if this works.

 writeComplex[file_String, data_List] := 
     Module[{str = OpenWrite[file, FormatType -> OutputForm]},
      Scan[Write[str, #] &, data];


writeComplex["path/to/your/file/filename.dat", dtList]


I looked up the format for complex numbers in Matlab and it seems to be of the form x + yi. Given this knowledge only a small modification is needed to the above code:

writeComplex2[file_String, data_List] := 
 Module[{str = OpenWrite[file, FormatType -> OutputForm]}, 
  Scan[Write[str, #] &, data /. {Complex[x_, y_] :> ToString[x] <> "+" <> ToString[y] <> "i"}]; 
  • 1
    $\begingroup$ Why shouldn't Export["test.mat",dtList,"MAT"] not work? See here the doc. $\endgroup$ – halirutan Nov 7 '13 at 4:25
  • $\begingroup$ @halirutan. I'm not doubting that it will work. My guess is, the OP is dealing with large numerical data, in which case Export will be extremely slow, see for example mathematica.stackexchange.com/questions/35371/… $\endgroup$ – RunnyKine Nov 7 '13 at 4:27
  • $\begingroup$ Did you test this? Your function needs 8 seconds for one million complex values while Export to MATLAB runs in less than one second! $\endgroup$ – halirutan Nov 7 '13 at 4:32
  • $\begingroup$ @halirutan. I just realized that this specific format is binary. I feel really stupid now. Of course, this is not a fair comparison. BinaryWrite should be used in this case. $\endgroup$ – RunnyKine Nov 7 '13 at 4:36
  • $\begingroup$ @halirutan. You should post it as the answer then. It'll probably be faster than anything else unless BinaryWrite is used. $\endgroup$ – RunnyKine Nov 7 '13 at 4:47

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.