简体   繁体   中英

Stop mex function (C) without closing MATLAB

I want to stop the execution of my C code after I detected an NaN and send the error message to MATLAB using mexWarnMsgTxt.

The C code is executed from MATLAB over mex file. I tried to use abort() and exit() which indeed kill the c program but also MATLAB (I guess because it is the calling process here.)

#include "mex.h"
#include "matrix.h"


for (int i = 0; i <= 5; i++)
       {
           if (mxIsFinite(out[i])) {

           }
           else if (mxIsInf(out[i])) {
               char *err_msg = malloc(max_len_err_msg);
               snprintf(err_msg, max_len_err_msg, "Inf detected in file %s at line %d", __FILE__, __LINE__);
               mexWarnMsgTxt(err_msg);
               abort();
               //free(err_msg);
               //abort(1);
               /* NOTE: Test for NaN is here for illustration only.  If a double
                * is not finite and is not infinity, then it is a NaN */

           }
           else if (mxIsNaN(out[i])) {
               char *err_msg = malloc(max_len_err_msg);
               snprintf(err_msg, max_len_err_msg, "NaN detected in file %s at line %d", __FILE__, __LINE__);
               mexWarnMsgTxt(err_msg);
               abort();
               //free(err_msg);

           }
       }

I just want my mexFunction to stop but not to terminate Matlab.

mex functions are normal C functions so, to leave the function early, just use return .

If you function allocated resources that need to be cleaned up manually, the established idiom in C is to use a goto cleanup; (this is one of the few, if not the only, acceptable and generally accepted uses of goto ):

void mexFunction(
    int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[]
) {
    for (int i = 0; i <= 5; i++) {
        if (mxIsInf(out[i])) {
            char *err_msg = malloc(max_len_err_msg);
            snprintf(err_msg, max_len_err_msg, "Inf detected in file %s at line %d", __FILE__, __LINE__);
            mexWarnMsgTxt(err_msg);
            free(err_msg);
            goto cleanup;
        } else if (mxIsNaN(out[i])) {
            char *err_msg = malloc(max_len_err_msg);
            snprintf(err_msg, max_len_err_msg, "NaN detected in file %s at line %d", __FILE__, __LINE__);
            mexWarnMsgTxt(err_msg);
            free(err_msg);
            goto cleanup;
        }
        …
    }

cleanup:

    // Perform cleanup here.
}

(Note that in this code the err_msg cleanup is performed in its own scope rather than with the global cleanup.)

But in the case where no cleanup is to be performed, the goto statements are unnecessary and can be replaced by return .

My current solution is to define a global variable abort_flag in C, set it to 1 if the error occured and based on that break all my loops and return from the functions. Kind of "manual" but works:

int  abort_flag = 0;
// to use in other file insert into header: extern int abort_flag;

// in the NaN case (see above)
abort_flag = 1;

// in the loops
if (abort_flag==1) { break; };

// in the functions
if (abort_flag==1) { return; };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM