MATLAB: How to specify the own print and error handling functions using the MCC Command when generating a standalone executable using MATLAB Compiler 4.10 (R2009a)

initializewithhandlersMATLAB Compilermcldefaulterrorhandlermcldefaultprinthandler

I am creating a standalone executable from my MATLAB function called "myfunc.m" using the MCC command using MATLAB Compiler 4.10 (R2009a). I would like to use my own custom printing and error handling routines in my application.
In order to do this, it appears that I have to modify the C wrapper files that are generated and re-build them to create the executable.
I would like to be able to specify my custom print and error handling routines using an option switch in the MCC command.

Best Answer

The ability to specify custom print and error handling routines using options switches for the MCC command is not is not available in MATLAB Compiler.
To work around this issue, you would have to generate the wrapper files, modify them and then re-compile them into an executable. The steps below indicate how you can do this.These steps assume that you are attempting to create a standalone from the MATLAB function "myfunc.m".
1. Generate the C wrapper files for the function myfunc.m using the commands below:
mcc -W main foo.m -C
This generates three C-files called "myfunc_main.c", "myfunc_delay_load.c" and "myfunc_mcc_component.c". Do not embed the CTF as once it is embedded it will be deleted.
2. Open the file "myfunc_main.c". This file contains the following line of code
LIB_myfunc_C_API
bool MW_CALL_CONV myfuncInitialize(void)
{
return myfuncInitializeWithHandlers(mclDefaultErrorHandler, mclDefaultPrintHandler);
}
where "mclDefaultErrorHandler" and "mclDefaultPrintHandler" are the default routines.
3. Add the C-code for your custom print and error handling functions in the file "myfunc_main.c". Let these functions be called "myPrintHandler" and "myErrorHandler" respectively. If these functions are defined in another source file, please be sure to include the header file containing the function prototype in the file "myfunc.c".
4. Please modify the the code specified above to the following:
LIB_myfunc_C_API
bool MW_CALL_CONV myfuncInitialize(void)
{
return myfuncInitializeWithHandlers(myErrorHandler, myPrintHandler);
}
5. Compile the C-source files using the MBUILD command as shown below:
mbuild myfunc_delay_load.c myfunc_main.c myfunc_mcc_component_data.c -output foo
If the print and error handlers are present in another source file, please include that file also as an input to MBUILD.