【问题标题】:Writing an extremely basic mex function in matlab在matlab中编写一个非常基本的mex函数
【发布时间】:2016-06-01 04:20:24
【问题描述】:

我正在尝试编写一个非常简单的 mex 文件,假设只是为了尝试它的工作方式。我浏览了很多资料,我读的越多,我就越困惑。我需要这个来进一步编写一个与外部硬件交互的 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 有一个自己的“控制台”,他们称之为“命令窗口”。这意味着您的 cout 输出将不会在 Matlab 中看到。

标签: c++ matlab mex


【解决方案1】:

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

mexPrintf("NAME\n");

【讨论】:

    猜你喜欢
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多