从 mex 文件和另一个线程打印到 MATLAB 命令窗口

问题描述

我修改了一个示例程序来演示这个问题:它每秒打印一个字符串。 问题是在 MATLAB 返回空闲状态(一次全部)后,消息出现在命令窗口中。 如果从命令行调用它并且 MATLAB 返回空闲状态,则消息会出现在命令行窗口中:

mexFile;

但在这种情况下:

mexFile; while 1; end;

在我通过 ^C 中止无限循环后出现消息。

这是我的源代码。它使用 feval("fprintf"...)。对 pause(0.001)drawnow 的后续调用不会改变任何内容。

如何强制 matlab 将消息“刷新”到命令窗口?

#include "mex.hpp"
#include "mexAdapter.hpp"
#include <thread>
#include <future>    


class MexFunction : public matlab::mex::Function {
private:
    std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
public:
    void print(const std::string& msg)
    {
        matlab::data::ArrayFactory factory;
        // Pass stream content to MATLAB fprintf function
        matlabPtr->fevalAsync(u"fprintf",std::vector<matlab::data::Array>({ factory.createScalar(msg) }));
        // matlabPtr->evalAsync(u"pause(0.001);");
    }

    void DisplayDateTime() {
        matlab::data::ArrayFactory factory;
        for (int count = 0; count < 20; count++)
        {
            print("Loop " + std::to_string(count) + "\n");
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    }

    void operator()(matlab::mex::ArgumentList outputs,matlab::mex::ArgumentList inputs) {
        mexLock();
        voidStdFuture = std::async(std::launch::async,&MexFunction::DisplayDateTime,this);
    }
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...