rsmenon / MATLink

Communicate with MATLAB from Mathematica
matlink.org
68 stars 14 forks source link

engEvalaute[] does not return proper UTF-8 when using R2013a #9

Open szhorvat opened 11 years ago

szhorvat commented 11 years ago

Screen Shot 2013-03-31 at 14 33 58

Notice the "!" where we should see "中". When using 2012b on OS X I get the correct "中".

szhorvat commented 11 years ago

It seems this works correctly on:

It fails on:

Sending UTF-8 data to engEvaluate[] may crash on Windows R2013a which makes this issue more serious than just an annoyance.

szhorvat commented 11 years ago

To work around this on Windows (where it may cause crashes!) it may be necessary to wrap everything sent to MEvaluate[] in eval or evalc, like this (siplified):

MEvaluate[code_] := (MSet["code", code]; engEvaluate["s = evalc(code);"]; engEvaluate["clear code"]; MGet["s"])

Do you see any potential problems with this approach? Performance hits maybe?

Unicode in scripts may need to be handled differently.

szhorvat commented 11 years ago

Using evalc() disables JIT compilation and makes performance significantly worse. Thus it's not a viable solution.

Pasteable test code:

mEvaluate[code_String] := (MSet["code", code]; 
  MATLink`Engine`engEvaluate["result = evalc(code); clear code"]; 
  MGet["result"])

mEvaluate[" x=zeros(1e7,1);
  tic;
  for i=1:1e7
  x(i)=rand();
  end
  toc"]

MEvaluate[" x=zeros(1e7,1);
  tic;
  for i=1:1e7
  x(i)=rand();
  end
  toc"]
szhorvat commented 11 years ago

Worked around by using evalc() https://github.com/rsmenon/MATLink/commit/d5fe5437c443c108eb859e105926bd0cb81706a7

rsmenon commented 11 years ago

Since we reverted evalc for now (because it prints HTML errors & help), I'm reopening this. Also, it crashes on my OS X 10.8.3, MATLAB 2013a

szhorvat commented 11 years ago

Can you elaborate on the crash, and give an example to reproduce it?

rsmenon commented 11 years ago

MEvaluate["x='aéîøü'"] is sufficient to crash it. It gives a LinkObject::linkd error (which btw, is also on my todo list for today to return sensible errors)

szhorvat commented 11 years ago

It looks like unicode wouldn't work with several MATLAB or OS versions in the initial release anyway, so I am sending data to Mathematica as ASCII now. This will at least prevent crashes similar to what you saw.

https://github.com/rsmenon/MATLink/commit/fcceb71ef9387e2a7745c4737e733f481624844c

(binary not checked in for now, you'll need to recompile yourself)