【发布时间】: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()。请参阅下面的答案。