【问题标题】:Clear MATLAB command window from mex function从 mex 函数中清除 MATLAB 命令窗口
【发布时间】:2017-05-31 17:05:17
【问题描述】:

我在mexFunction 中有一个for 循环,它会在每次迭代时显示一些信息。考虑这个简单的代码,它将在 MATLAB 命令窗口中打印 100 行并在每次迭代时更新:

#include "mex.h"
#include <iostream>
#include <sstream>
#include <stdio.h>      
#include <stdlib.h>     
#include <time.h>       

using namespace std;

/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[])
{
    int numiters = 100;
    /* initialize random seed: */
    srand(time(NULL));

    for (int iter = 1; iter <= numiters; ++iter)
    {
        int rand_num = rand() % 100 + 1;

        /* Print info in matlab */
        std::ostringstream buffer;
        buffer << "iter = " << iter << " of " << numiters << 
            ". random number = " << rand_num << endl;

        /* need something similar to clc here */
        mexPrintf("%s", buffer.str().c_str());
    }
    return;
}

在每次迭代中,我希望在调用 mexPrintf() 之前清除 MATLAB 的命令窗口。

我知道我可以使用 mexCallMATLAB 来调用 MATLAB 的 clc,但我不确定在每次迭代时调用 MATLAB 是否非常有效,因此我需要一个 C++ 原生的解决方案。

【问题讨论】:

  • “我不确定在每次迭代中调用 MATLAB 是否非常有效” 可能是,无论如何你都在通过 mexPrintf() 调用它。我建议你试试看。
  • @AnderBiguri。有道理。我在mexPrintf() 之前插入了mexCallMATLAB(0, NULL, 0, NULL, "clc");,但没有任何效果。不知道为什么。
  • 我意识到它为什么不起作用。我错过了使用ioFlush()。请参阅下面的答案。

标签: c++ matlab mex


【解决方案1】:

我对@9​​87654322@ 效率低下的假设是错误的,感谢@Ander Biguri 的提醒。我使用以下测试代码比较了有和没有mexCallMATLAB 的时间。


TL;DR - 计时结果

  • mexCallMATLAB(0, NULL, 0, NULL, "clc");:mean time per iteration = 0.1016885 sec
  • 没有mexCallMATLAB(0, NULL, 0, NULL, "clc");: mean time per iteration = 0.0978730 sec

详情

测试台总结

  • 用一百万个随机整数创建一个向量并取平均值。
  • 重复 500 次迭代。
  • 计算每次迭代所需的时间并保存在向量中。
  • 平均迭代时间以获得每次迭代的平均时间。

代码

#include "mex.h"
#include <iostream>
#include <sstream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <ctime>
#include <vector>

using namespace std;
extern bool ioFlush(void);

/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[])
{
    int numiters = 500;
    /* initialize random seed: */
    srand(time(NULL));

    std::vector<double> all_times(numiters);

    for (int iter = 1; iter <= numiters; ++iter)
    {
        /* tic */
        clock_t begin = clock();

        std::vector<int> rand_vec;

        for(std::size_t i = 0; i < 1000000; ++i)
            rand_vec.push_back( rand() % 100 + 1 );

        double vec_mean = 0.0;
        for(std::size_t i = 0; i < rand_vec.size(); ++i)
            vec_mean += rand_vec[i];

        vec_mean /= rand_vec.size();

        /* clear screen */
        mexCallMATLAB(0, NULL, 0, NULL, "clc");

        /* toc */
        double time_elapsed = double(clock() - begin) / CLOCKS_PER_SEC;

        /* Print data in matlab */
        std::ostringstream buffer;
        buffer << "iter " << iter << " of " << numiters << 
                ". random vector mean = " << vec_mean << 
                ". in " << time_elapsed << "s" << endl;

        mexPrintf("%s", buffer.str().c_str());
        ioFlush();

        all_times[iter] = time_elapsed;
    }

    double avg_time = 0.0;
    for(std::size_t i = 0; i < all_times.size(); ++i)
        avg_time += all_times[i];

    avg_time /= all_times.size();
    mexPrintf("\navg time per iter = %3.7f\n", avg_time);

    return;
}

注意ioFlush() 的使用,这是一个未记录的函数,需要在每次迭代时更新命令窗口。查看有关它的更多详细信息here

编译

  • 在使用 Visual Studio 的 Windows 上:在源文件中添加 #pragma comment(lib, "libmwservices.lib")
  • 在使用 gcc 的 Ubuntu 上:使用 mex yourFile.cpp -lmwservices 编译

【讨论】:

  • 干得好,答案很好。
猜你喜欢
  • 2022-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多