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

查看:26
本文介绍了每次迭代发送一个矩阵: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:

矩阵.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.

推荐答案

如果不知道先验帧数,就不要尝试在C中扩展mxArray,不方便.你已经接近开始了.您的所有问题都可以通过以下方式解决:

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")

阅读更多这里.

最简单的方法是这样的:

The simplest approach is something like:

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天全站免登陆