Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing an extremely basic mex function in matlab

Tags:

c++

matlab

mex

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?

like image 774
Ashwini Avatar asked Jun 01 '16 04:06

Ashwini


1 Answers

The call to cout will not print to the MATLAB console, you need to use the MEX printf function.

mexPrintf("NAME\n");
like image 58
CJCombrink Avatar answered Sep 22 '22 02:09

CJCombrink