每次迭代发送一个矩阵:Matlab“engine.h” c++

发布于 2024-12-27 01:58:45 字数 461 浏览 3 评论 0原文

这个问题是在解决我在这个问题中遇到的问题之后提出的。我有一个 C++ 代码,用于处理来自相机的帧并为每个处理后的帧生成一个矩阵。我想将每个矩阵发送到 matlab 引擎,因此在执行结束时我存储了所有矩阵。 我对如何做到这一点感到困惑,我在每次迭代中发送一​​个矩阵,但它总是覆盖它,所以最后我只有一个。这是一个代码示例:

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 引擎要求首先定义变量。任何帮助将不胜感激。谢谢。

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

#include helper.h

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初与友歌 2025-01-03 01:58:45

您可以在 matlab 工作区中创建元胞数组,如下所示:

    mwSize size = 10;
    mxArray* cell = mxCreateCellArray(1, &size);

    for(size_t i=0;i<10;i++)
    {
        mxArray *mat;   
        mat = mxCreateDoubleMatrix(13, 13, mxREAL);     
        memcpy(mxGetPr(mat),matrix.data, 13*13*sizeof(double));

        mwIndex subscript = i;
        int index = mxCalcSingleSubscript(cell , 1,&subscript); 
        mxSetCell(m_cell , index, mat);
   }

   engPutVariable(engine, "myCell", cell);

You can create a cell array in matlab workspace like this:

    mwSize size = 10;
    mxArray* cell = mxCreateCellArray(1, &size);

    for(size_t i=0;i<10;i++)
    {
        mxArray *mat;   
        mat = mxCreateDoubleMatrix(13, 13, mxREAL);     
        memcpy(mxGetPr(mat),matrix.data, 13*13*sizeof(double));

        mwIndex subscript = i;
        int index = mxCalcSingleSubscript(cell , 1,&subscript); 
        mxSetCell(m_cell , index, mat);
   }

   engPutVariable(engine, "myCell", cell);
ぇ气 2025-01-03 01:58:45

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

engEvalString(engine, "your command here")

此处了解更多信息。

最简单的方法是这样的:

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

不要完全这样做,它只是一个示例,速度会非常慢。最好预先分配,例如 1000 个帧,然后在需要时再扩展 1000 个(或更合适的数字)。更好的是不要使用速度慢的单元阵列。相反,您可以使用 3D 数组,例如:

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

再次,按块预分配。你明白了。如果您确实需要更快,您可以用 C 语言构建 3D 数组,并在填充时将它们发送到 MATLAB。

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

Read more here.

The simplest approach is something like:

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

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;

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.

远昼 2025-01-03 01:58:45

也许您可以使用 stdlib 中的 vector

Maybe you can use vector<mxArray> from stdlib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文