Hi all,
In Matlab, if a function is running and you press the keys Ctrl+C, the
function stops its execution and exits with an error. This is perfectly
fine, desirable behaviour.
With C++ MEX functions, this doesn't work by default, which is a real
pain when you have e.g. a loop with lots of iterations that will take
for a very long time to complete. If one launches the MEX function, and
realises a parameter is wrong, the only way to stop execution is by
forcibly killing Matlab itself and restarting everything again. (Or you
have to wait until the function finishes).
Prof. Wotao Yin has kindly written a short tutorial where he explains
that there's an undocumented library in Matlab with a function
utIsInterruptPending() that captures the Ctrl+C signal
http://www.caam.rice.edu/~wy1/links/mex_ctrl_c_trick/
I have added a function to Gerardus that takes advantage of this
feature. The function is called ctrlcCheckPoint()
http://code.google.com/p/gerardus/source/browse/trunk/matlab/GerardusCommon.hpp#67?r=953
and can be included in your C++ code like this:
for (size_t i=0; i<1e6; ++i) {
// exit if user pressed Ctrl+C
ctrlcCheckPoint(__FILE__, __LINE__);
... all your loop code here ...
}
This way, at the beginning of each loop iteration, your program will
check whether the user has pressed Ctrl+C. If so, it produces an error
message like this in Matlab, and exits:
>> Operation terminated by user during cgal_insurftri (line 336)
In addition, the error message contains two hyperlinks:
* Clicking on "cgal_insurftri" will display the help for that function.
* Clicking on "line 336" will open the source file
CgalInSurfaceTriangulation.cpp at line 336 (where ctrlcCheckPoint() was
called from)
A practical example can be found in the code for the MEX function
cgal_insurftri(), where I have added check points in three loops:
http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp#230?r=953
http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp#335?r=953
http://code.google.com/p/gerardus/source/browse/trunk/matlab/CgalToolbox/CgalInSurfaceTriangulation.cpp#369?r=953
Cheers,
Ramon.
--
Dr. Ram�n Casero Ca�as
Oxford e-Research Centre (OeRC)
University of Oxford
7 Keble Rd
Oxford OX1 3QG
tlf
+44 (0) 1865 610739
web
http://www.cs.ox.ac.uk/people/Ramon.CaseroCanas
photos
http://www.flickr.com/photos/rcasero/