Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
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 7 '13 at 4: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 7 '13 at 18:04
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
Yes, MATLink does support transferring complex numbers to/from MATLAB :-) –  Szabolcs Nov 7 '13 at 18:04
add comment

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];
]
share|improve this answer
1  
Why shouldn't Export["test.mat",dtList,"MAT"] not work? See here the doc. –  halirutan Nov 7 '13 at 4: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 mathematica.stackexchange.com/questions/35371/… –  RunnyKine Nov 7 '13 at 4: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 7 '13 at 4: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 7 '13 at 4:36
    
@halirutan. You should post it as the answer then. It'll probably be faster than anything else unless BinaryWrite is used. –  RunnyKine Nov 7 '13 at 4:47
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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