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

1 visualización (últimos 30 días)
Fred
Fred el 24 de Sept. de 2015
Comentada: Fred el 28 de Sept. de 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?

Respuestas (1)

Rahul Goel
Rahul Goel el 28 de Sept. de 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 comentario
Fred
Fred el 28 de Sept. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Deploy to C++ Applications Using mwArray API (C++03) en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by