Documentation

matlab::cpplib::runMain

Execute a function with its input arguments within the main function

Description

int runMain(std::function<int(std::shared_ptr<MatlabApplication>, int, const char**)>std::shared_ptr<MatlabApplication>&& appsession, 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 is used specially on macOS to fulfill the requirements of the Cocoa API. It can be used on Windows® and Linux® platforms as well.

Parameters

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

int

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

Examples

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(
            mc::MATLABApplicationMode::IN_PROCESS,options);
      return mc::runMain(myMainFunc, std::move(matlabApplication), argc, argv);
  }

Introduced in R2018a