4

I'm using the MATLAB Engine C interface on OS X. I noticed that if engEvalString() is given an incomplete MATLAB input such as

engEvalString(ep, "x=[1 2");

or

engEvalString(ep, "for i=1:10");

then the function simply never returns. The quickest way to test this is using the engdemo.c example which will prompt for a piece of MATLAB code and evaluate it (i.e. you can type anything).

My application lets the user enter arbitrary MATLAB input and evaluate it, so I can't easily protect against incomplete input. Is there a workaround? Is there a way to prevent engEvalString() from hanging in this situation or is there a way to check an arbitrary piece of code for correctness/completeness before I actually pass it to MATLAB?

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • Perhaps you could add a time limit in c? – Dennis Jaheruddin Feb 06 '13 at 10:38
  • @Szabolcs: you deleted your [question](http://stackoverflow.com/questions/14942097/accessing-matlabs-unicode-strings-from-c) from yesterday before I got a chance to post my answer. Can you please reopen it, I had a few ideas to share :) – Amro Feb 19 '13 at 16:10
  • @Amro I realized that I was stupid, and I got a reply on MATLAB Answers. I shouldn't have used mxArrayToString at all: just get the mxChar data from the array: mxChar is a two-byte type and contains unicode data. Do you still want me to reopen ? – Szabolcs Feb 19 '13 at 16:20
  • @Szabolcs: yes please. I show a couple of undocumented functions for unicode strings.. – Amro Feb 19 '13 at 16:25
  • @Amro Sounds interesting. [Done!](http://stackoverflow.com/questions/14942097/accessing-matlabs-unicode-strings-from-c) – Szabolcs Feb 19 '13 at 16:29

2 Answers2

1

As you noted, it seems this bug is specific to Mac and/or Linux (I couldn't reproduce it on my Windows machine). As a workaround wrap the calls in eval, evalc, or evalin:

engEvalString(ep, "eval('x = [1,2')")

Furthermore, an undocumented feature of those functions is that they take a second input that is evaluated in case an error occurs in the first one. For example:

ERR_FLAG = false;
eval('x = [1,2', 'x=nan; ERR_FLAG=true;')

You can trap errors that way by querying the value of a global error flag, and still avoid the bug above...

Amro
  • 123,847
  • 25
  • 243
  • 454
0

This was confirmed by support to be a bug in the MATLAB Engine interface on OS X (it's not present in Windows). Workarounds are possible by using the MATLAB functions eval, evalc, or similar. Instead of directly passing the code to engEvalString(), wrap it in these first.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174