简体   繁体   中英

Writing an extremely basic mex function in matlab

I am trying to to write a very simple mex file, let's say just to try out the way it works. I have gone through a lot of materials and more I read, more I get confused. I need this to further write a mex file that interacts with external hardware. Please help!

// header file - printing.h //

#include<iostream>
class printing
{
public:

    void name();
    void age();
};

// cpp file - printing.cpp //
#include<iostream>
#include "mex.h"
#include "matrix.h"
#include "printing.h"
#include <string>

using namespace std;

void mexFunction(int nlhs, mxArray*plhs[],
                 int nrhs, const mxArray *prhs[])
{
   printing p1;
   p1.name();
   p1.age();

}

void printing::name()
{
    cout << "NAME" << endl;
}

void printing::age()
{
    cout << "20" << endl;

}

// .m file - test.m //

sprintf ('WELCOME')
printing()

When i run the test.m file, I would like to see WELCOME NAME 20 However I see only welcome. I understand that I have not updated the plhs[] array. But all I want to do is execute something inside mexFunction.Why wouldn't the cout inside name() and age() achieve this?

Also, how do i confirm that name() and age() are executed?

cout的调用不会打印到MATLAB控制台,您需要使用MEX printf函数。

mexPrintf("NAME\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM