c++ - 在 matlab 中编写一个非常基本的 mex 函数

标签 c++ matlab mex

我正在尝试编写一个非常简单的 mex 文件,假设只是为了尝试它的工作方式。我浏览了很多 Material ,阅读的越多,我就越困惑。我需要它来进一步编写一个与外部硬件交互的 mex 文件。请帮忙!

// 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 文件 - test.m//

sprintf ('WELCOME')
printing()

当我运行 test.m 文件时,我想看看 欢迎 姓名 20 但是我只看到欢迎。我知道我没有更新 plhs[] 数组。但我只想在 mexFunction 中执行一些操作。为什么 name() 和 age() 中的 cout 不能实现这一点?

另外,如何确认 name() 和 age() 被执行?

最佳答案

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

mexPrintf("NAME\n");

关于c++ - 在 matlab 中编写一个非常基本的 mex 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37559363/

相关文章:

c++ - 如何从成员对象的函数访问对象中的变量

c++ - float 的二进制序列化(仅限IPC)

c++ - 在 Windows 上的环回接口(interface)上使用 tcp/ip 的延迟方面可以预期什么?

c++ - 在 C/C++ 中将文件指针重定向到字符串

c++ - 为什么此代码(在 Matlab 的 MEX 文件中使用 OpenMP)给出不同的结果?

c++ - 多次调用mex函数后matlab无响应

c++ - 如何连接通过 cython 返回对象引用的 C++ 函数

matlab - 是否可以使 matlab GUIDE 应用程序中的文本可选择(并且可以复制)但不可编辑?

java - 计算函数的偏导数

c - C C++ 中的 matlab 和 matlab 中的 C C++