C++ 从mex函数中清除MATLAB命令窗口
我在C++ 从mex函数中清除MATLAB命令窗口,c++,matlab,mex,C++,Matlab,Mex,我在mexFunction中有一个for循环,它在每次迭代中显示一些信息。考虑这个简单的代码,它将在MATLAB命令窗口中打印100行,并在每次迭代时更新: #include "mex.h" #include <iostream> #include <sstream> #include <stdio.h> #include <stdlib.h> #include <time.h> using nam
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;
}
#包括“mex.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
/*网关功能*/
void MEX函数(整数nlhs,mxArray*plhs[],
整数nrhs,常量mxArray*prhs[]
{
int numiters=100;
/*初始化随机种子:*/
srand(时间(空));
对于(int iter=1;iter我关于mexCallMATLAB
效率低下的假设是错误的,这要感谢@Ander Biguri的提醒。我使用以下测试代码比较了使用和不使用mexCallMATLAB
的时间
时间太长了,读不下去了
- 使用
mexCallMATLAB(0,NULL,0,NULL,“clc”);
:每次迭代的平均时间=0.1016885秒
- 无
MEXCALLMAB(0,NULL,0,NULL,“clc”);
:每次迭代的平均时间=0.0978730秒
细节
试验台概要
- 创建一个包含100万个随机整数的向量并取其平均值
- 重复500次
- 计算每次迭代所需的时间并保存在vector中
- 平均迭代次数,以获得每次迭代的平均时间
代码
#包括“mex.h”
#包括
#包括
#include/*printf、scanf、put、NULL*/
#包括/*srand,兰特*/
#包括/*时间*/
#包括
#包括
使用名称空间std;
外部bool-ioFlush(无效);
/*网关功能*/
void MEX函数(整数nlhs,mxArray*plhs[],
整数nrhs,常量mxArray*prhs[]
{
整数=500;
/*初始化随机种子:*/
srand(时间(空));
std::矢量所有时间(单位);
对于(int iter=1;iter“我不确定在每次迭代时调用MATLAB是否非常有效”,不管怎样,您都是通过执行mexPrintf()
来调用它的。我建议您尝试并计时。@AnderBiguri.Fair point。我在mexPrintf()之前插入了mexCallMATLAB(0,NULL,0,NULL,clc”);
无效。不确定原因。我意识到为什么它不起作用。我使用ioFlush()
时丢失了它。请参阅下面的答案。干得好,答案不错。
#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;
}