How do I return an error to C++ from a compiled Matlab dll?

2 vues (au cours des 30 derniers jours)
Fred
Fred le 24 Sep 2015
Commenté : Fred le 28 Sep 2015
I have a Matlab function: foo=doFoo(); I have used the Matlab compiler to convert doFoo() to a function callable from C++: success=mlfDoFoo(int argOut, mxArray **foo); Every once in a while, doFoo crashes.
Is there a way to set success based on whether or not and how doFoo crashes?

Réponses (1)

Rahul Goel
Rahul Goel le 28 Sep 2015
Fred,
The syntax to use shared libraries in C++ code is such that their return type is "void" and hence cannot be used in the way you mentioned. Also, what exactly do you mean by "crash"? Did the whole program crashed or is it just the library which throws an error? All the outputs to be collected are passed as reference using the second argument("mxArray **foo" in this case) of the function called from shared library, if it crashes there will not be any variable which might store this error information.
I would suggest trying the try/catch in your code while using the shared library. An example can be found in the documentation at:
where a try/catch block is used to handle any exception thrown by the function called from the shared library:
try
{
// Create input data
double data[] = {1,2,3,4,5,6,7,8,9};
mwArray in1(3, 3, mxDOUBLE_CLASS, mxREAL);
mwArray in2(3, 3, mxDOUBLE_CLASS, mxREAL);
in1.SetData(data, 9);
in2.SetData(data, 9);
// Create output array
mwArray out;
// Call the library function
addmatrix(1, out, in1, in2);
std::cout << "The value of added matrix is:" << std::endl;
std::cout << out << std::endl;
}
catch (const mwException& e)
{
std::cerr << e.what() << std::endl;
return -2;
}
catch (...)
{
std::cerr << "Unexpected error thrown" << std::endl;
return -3;
}
Hope this helps.
  1 commentaire
Fred
Fred le 28 Sep 2015
Is the return type really void? As far as I could tell, success is a Boolean which indicates whether or not the function returned successfully. I ended up putting doFoo into a try-catch loop in Matlab and then setting an output error parameter based on the caught error.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Deploy to C++ Applications Using mwArray API (C++03) dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by