主要内容

matlab::cpplib::runMain

在主函数中执行带有输入参量的函数

描述

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

在主函数中执行带有输入参量的函数。matlab.cpplib.runMain 接受以下输入:您要执行的函数、一个 MATLABApplication 实例,以及该函数所需的输入。它返回一个代码作为输出,用于指示执行成功或失败。

此函数可在任何平台上使用,用于将主函数的逻辑与 main() 的逻辑相分离。在 macOS 上,它还满足 Cocoa API 的要求。

参数

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

一个 std::function 实例,它接受三个参数(即指向 MATLABApplication 对象的指针、表示输入参量数量的一个 int,以及表示输入参量本身的一个 const char**),并返回一个 int

std::shared_ptr<MATLABApplication>&& app

MATLABApplication 实例,作为 rvalue 传递。

int argc

来自命令行的输入参量的数量。

const char **argv

输入参量数组。

返回值

int

返回代码,用于指示成功(约定为 0)或失败(约定为非零数)。

示例

MATLABApplication 对象移动到 runMain 中并终止它

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);
  }

版本历史记录

在 R2018a 中推出