Main Content


Execute a function with its input arguments within the main function


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

Execute a function with its input arguments within the main function. matlab.cpplib.runMain accepts as input the function you want to execute, an instance of MATLABApplication, 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 of main(). On macOS, it also fulfills the requirements of the Cocoa API


std::function<int(std::shared_ptr<MATLABApplication>, int, const char**)> func

A std::function instance that takes three parameters (namely, a pointer to a MATLABApplication object, an int representing the number of input arguments, and a const char** representing the input arguments themselves) and returns an int.

std::shared_ptr<MATLABApplication>&& app

Instance of MATLABApplication, passed as rvalue.

int argc

Number of input arguments from the command line.

const char **argv

Input arguments array.

Return Value


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


Move the MATLABApplication Object into runMain and Terminate It

int myMainFunc(std::shared_ptr<mc::MATLABApplication> 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<std::u16string> options  ; 
      auto matlabApplication = mc::initMATLABApplication(
      return mc::runMain(myMainFunc, std::move(matlabApplication), argc, argv);

Version History

Introduced in R2018a