简体   繁体   English

在Matlab中编写极其基本的mex函数

[英]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. 我正在尝试编写一个非常简单的mex文件,让我们尝试一下它的工作方式。 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. 我需要它来进一步编写一个与外部硬件交互的mex文件。 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 // // .m文件-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. 当我运行test.m文件时,我希望看到WELCOME NAME 20,但是我只看到欢迎。 I understand that I have not updated the plhs[] array. 我知道我还没有更新plhs []数组。 But all I want to do is execute something inside mexFunction.Why wouldn't the cout inside name() and age() achieve this? 但是我要做的就是在mexFunction内部执行一些操作,为什么name()和age()内部的cout不能实现这一点?

Also, how do i confirm that name() and age() are executed? 另外,如何确认执行了name()和age()?

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

mexPrintf("NAME\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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