Sending a matrix with each iteration: Matlab quot;engine.hquot; c++(每次迭代发送一个矩阵: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++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:每次迭代发送一个矩阵:Matlab“engine.h";C++
基础教程推荐
- C++按值调用 1970-01-01
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01
- end() 能否成为 stl 容器的昂贵操作 2022-10-23
- C++ #define 1970-01-01
- 初始化变量和赋值运算符 1970-01-01
- C++输入/输出运算符重载 1970-01-01
- C++定义类对象 1970-01-01
- C语言访问数组元素 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- 使用scanf()读取字符串 1970-01-01