2

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

yulinlinyu
  • 4,815
  • 2
  • 29
  • 36
  • 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. – halirutan Nov 07 '13 at 04:10
  • 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. – Szabolcs Nov 07 '13 at 18:04

2 Answers2

2

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]

Update:

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.

halirutan
  • 112,764
  • 7
  • 263
  • 474
0

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];
      Close[str];
      ]

Usage

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

UPDATE

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"}]; 
  Close[str];
]
RunnyKine
  • 33,088
  • 3
  • 109
  • 176
  • 1
    Why shouldn't Export["test.mat",dtList,"MAT"] not work? See here the doc. – halirutan Nov 07 '13 at 04:25
  • @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 http://mathematica.stackexchange.com/questions/35371/speeding-up-import-and-export-in-csv-format/35375#35375 – RunnyKine Nov 07 '13 at 04:27
  • Did you test this? Your function needs 8 seconds for one million complex values while Export to MATLAB runs in less than one second! – halirutan Nov 07 '13 at 04:32
  • @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. – RunnyKine Nov 07 '13 at 04:36
  • @halirutan. You should post it as the answer then. It'll probably be faster than anything else unless BinaryWrite is used. – RunnyKine Nov 07 '13 at 04:47
  • @halirutan,yes,Export["test.mat",dtList] works right. But this doesn't work for Excel or orgin – yulinlinyu Nov 07 '13 at 04:57
  • @RunnyKine, 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. Thank you all the same. By the way, what do you mean by BinaryWrite? Dos it generate a file? I'll grateful if you can provide an example – yulinlinyu Nov 07 '13 at 06:19