每次迭代发送矩阵:Matlab“engine.h” c ++ [英] Sending a matrix with each iteration: Matlab "engine.h" c++

查看:191
本文介绍了每次迭代发送矩阵:Matlab“engine.h” c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是解决我在此问题。我有一个c ++代码,处理来自相机的帧,并为每个处理帧生成一个矩阵。我想发送给matlab引擎每个矩阵,所以在执行结束时我已经存储了所有的矩阵。
我被告知如何做到这一点,我在每次迭代发送一个矩阵,但它是覆盖它所有的时间,所以最后我只有一个。以下是代码示例:

This question comes after solving the problem I got in this question. I have a c++ code that processes frames from a camera and generates a matrix for each processed frame. I want to send to matlab engine each matrix, so at the end of the execution I have in stored all the matrices. I am conffused about how to do this, I send a matrix in each iteration but it is overwritting it all the time, so at the end I only have one. Here is a code example:

matrix.cpp

matrix.cpp

#include helper.h

mxArray *mat;   
mat = mxCreateDoubleMatrix(13, 13, mxREAL);     
memcpy(mxGetPr(mat),matrix.data, 13*13*sizeof(double));
engPutVariable(engine, "mat", mat);

我也尝试使用一个计数器来命名不同的矩阵,但它不工作matlab引擎需要首先定义变量。任何帮助将不胜感激。谢谢。

I also tried to use a counter to dinamically name the different matrices, but it didn't work as matlab engine requires the variables to be defined first. Any help will be appreciated. Thanks.

推荐答案

如果你不知道先前的帧数,不要尝试扩展mxArray在C这不方便。你已经接近开始。您所有的问题都可以用以下方法解决:

If you don't know the number of frames a priori, don't try to expand the mxArray in C. It is not convenient. You were already close to start. All your problems can be solved with:

engEvalString(engine, "your command here")

阅读更多此处

最简单的方法是:

engPutVariable(engine, "mat", mat);
engEvalString("frames{length(frames)+1} = mat;");

不要这样做,非常慢。更好地预分配,说1000帧,然后展开它另一个1000(或更适当的数量),当需要时。更好的是不使用缓慢的单元阵列。您可以使用3D数组,例如:

Don't do it exactly that, it is an illustration and will be very slow. Much better to preallocate, say 1000 frames then expand it another 1000 (or a more appropriate number) when needed. Even better is to not use cell arrays which are slow. Instead you could use a 3D array, such as:

frames = zeros(13,13,1000);
frames(:,:,i) = mat;
i = i + 1;

再次,以块为单位预分配。你得到的想法。如果你真的需要快速,你可以在C中构建3D数组,并在他们填充时将它们发送到MATLAB。

Again, preallocate in blocks. You get the idea. If you really need to be fast, you could build the 3D arrays in C and ship them to MATLAB when they fill.

这篇关于每次迭代发送矩阵:Matlab“engine.h” c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆