Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a matrix with each iteration: Matlab "engine.h" c++

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.

like image 881
Jav_Rock Avatar asked Dec 17 '22 05:12

Jav_Rock


1 Answers

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);
like image 169
Philipp Avatar answered Jan 06 '23 20:01

Philipp