Main Content

matlab::cpplib::runMain

Execute a function with its input arguments within the main function

Description

int runMain(std::function func, int, const char**)>std::shared_ptr&& app, int argc, const char **argv);

Execute a function with its input arguments within the main function.matlab.cpplib.runMainaccepts as input the function you want to execute, an instance ofMATLABApplication, and the inputs to the function you want to execute. It returns as output a code indicating the success or failure of execution.

This function can be used on any platform to separate the logic of the primary function from that ofmain(). OnmacOS, it also fulfills the requirements of the Cocoa API

Parameters

std::function, int, const char**)> func

Astd::functioninstance that takes three parameters (namely, a pointer to aMATLABApplicationobject, anintrepresenting the number of input arguments, and aconst char**representing the input arguments themselves) and returns anint.

std::shared_ptr&& app

Instance ofMATLABApplication, passed asrvalue.

int argc

Number of input arguments from the command line.

const char **argv

Input arguments array.

Return Value

int

Return code indicating success (by convention:0), or failure (by convention, a non-zero number).

Examples

Move theMATLABApplicationObject intorunMainand Terminate It

int myMainFunc(std::shared_ptr app, const int argc, const char * argv[]) { try { // initialize library, call feval, etc. } catch(const std::exception & exc) { std::cerr << exc.what() << std::endl; return -1; } return 0; // no error } int main(const int argc, const char * argv[]) { std::vector options ; auto matlabApplication = mc::initMATLABApplication( mc::MATLABApplicationMode::IN_PROCESS,options); return mc::runMain(myMainFunc, std::move(matlabApplication), argc, argv); }

Version History

Introduced in R2018a