1

I'm trying to take some C++ code and place the appropriate MatLab mex function wrappers around it so that I can call my C++ as a MatLab function. My C++ code takes in 4 command line arguments using argc and argv.

The C++ function call from the command line would be: myfunciton string1 string2 string3 string4

What I want is my MatLab function call from the MatLab command line to look like: myfunction('string1', 'string2', 'string3', 'string4')

I've looked over the following blog post and the OP is able to do this using c (not c++) and between that and the MatLab documentation for C++ I've been a little confused (mostly attributed to differences in Syntax between the Blog Post and Mathwork docs for c++)

https://sungkwang.wordpress.com/2011/01/17/passing-argument-in-mex-function-without-modifying-c-code/

https://www.mathworks.com/help/matlab/matlab_external/c-mex-source-file.html

#include "mex.hpp"
#include "mexAdapter.hpp"

using namespace matlab::data;
using matlab::mex::ArgumentList;

class MexFunction : public matlab::mex::Function {
public:
    void operator()(ArgumentList outputs, ArgumentList inputs) {
       int argc = 0;
       argc = inputs.size();
       //I'm unsure of how to handle the argv portion(?)
       main(argc,argv);

    }


    int main(int argc, char *argv[]){
       //Rest of code... omitted for brevity of question
    }
};

sdub0800
  • 139
  • 1
  • 9
  • You can use the C MEX API to call your C++ code, you don't need to use the new C++ API. Though, arguably, the C++ API is easier to use, there are many fewer examples for it as of yet. – Cris Luengo Apr 10 '19 at 14:22

1 Answers1

0

Copy this into your command window:

edit([matlabroot '/extern/examples/cpp_mex/arrayProduct.cpp']);

You can find some other examples here

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Hannah
  • 1
  • 1