フィルターのクリア

How to throw an exception when using MATLAB C or C++ API

10 ビュー (過去 30 日間)
Ziyu Hua
Ziyu Hua 2022 年 2 月 11 日
コメント済み: Ziyu Hua 2023 年 10 月 9 日
I have a C++ code, which is like:
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
try{
// Here is my code
}catch(...){
mexErrMsgTxt("Something Wrong");
}
}
After mex the code, I want to get "Something Wrong" in MTALAB window if any exception was throwed from the "try" part.
However, if there do some errors in the code (inside the try curly bracket), it will crush my MATLAB like
The try-catch seems ineffective in this stuation. So how to throw an exception from C++ to MATLAB if anything wrong?

採用された回答

Aiswarya
Aiswarya 2023 年 10 月 3 日
Hi,
I understand that you are trying to throw an exception from C++ to MATLAB after converting the code to a MEX function.
This displays the error in the MATLAB command window.
You may refer to a C++ example code (mexcpp.cpp) which implements error handling by running the following command in the MATLAB Command Window:
edit([matlabroot '/extern/examples/mex/mexcpp.cpp']);
  1 件のコメント
Ziyu Hua
Ziyu Hua 2023 年 10 月 9 日
Thanks, I re-compile the code below like this and it seems to be fine.
#include"mex.h"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
try {
// Here is my code
throw(1);
}
catch (...) {
mexErrMsgIdAndTxt("error","Something Wrong");
}
}
The output of the MATLAB is an error message.
I can't remember what particular problems I once met, but it seems that they have been resolved.
Thanks.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by