how to check Ctrl+C status in a mex cpp program

4 vues (au cours des 30 derniers jours)
Mingmin
Mingmin le 6 Juin 2025
Commenté : Mingmin le 16 Juin 2025
When I compile a c++ example of mex (dlib-20.0\dlib\matlab), an error occurs of the following codes
extern "C" bool utIsInterruptPending();
void check_for_matlab_ctrl_c(){
if (utIsInterruptPending())
throw mex_binding::user_hit_ctrl_c();
}
It seems that there isn't a function named "utIsInterruptPending" in matlab. Then how can we get the Ctrl+C status in a mex file?
  2 commentaires
Mingmin
Mingmin le 8 Juin 2025
thank you very much for answering. However, the source code you offered is:
inline static bool utIsInterruptPending() { return false; }
It is not a feature provided by MATLAB (perhaps it used to be).

Connectez-vous pour commenter.

Réponse acceptée

Umeshraja
Umeshraja le 16 Juin 2025
Modifié(e) : Umeshraja le 16 Juin 2025
I found the utIsInterruptPending function in the libut.lib library, which can be accessed at the following location:
<MATLAB PATH>\extern\lib\win64\mingw64\libut.lib
I assume you press Ctrl+C during a mexCallMATLAB or mexCallMATLABWithTrap. To enable this functionality, make sure to include the following at the top of your MEX-file:
#ifdef __cplusplus
extern "C" {
#endif
extern bool utIsInterruptPending(void);
#ifdef __cplusplus
}
#endif
Further you will need to link against libut.lib, so the MEX command becomes:
mex myFunction.c libut.lib
The following shows a simple MEX-file which uses utIsInterruptPending:
#include "mex.h"
/* utIsInterruptPending prototype */
#ifdef __cplusplus
extern "C" {
#endif
extern bool utIsInterruptPending(void);
#ifdef __cplusplus
}
#endif
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]) {
/* Call the PAUSE function in MATLAB */
if (mexCallMATLAB(0, NULL, 0, NULL, "pause")) {
mexErrMsgTxt("Error evaluating callback function!");
}
if (utIsInterruptPending()) { /* <== Check if utIsInterruptPending (someone pressed Ctrl+C) */
mexPrintf("Ctrl+C was pressed!\n");
return;
}
mexPrintf("MEX file completed normally.\n");
}
I hope this helps!
  1 commentaire
Mingmin
Mingmin le 16 Juin 2025
Thank you very much! It works well now!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange

Produits


Version

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by