Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
index of matrix in mex-file

Subject: index of matrix in mex-file

From: Jan Widmann

Date: 26 Apr, 2009 18:22:01

Message: 1 of 4

hi all,

probably this is one stupid question, but i couldn't find a solution all day long...

i had a c/c++ code and changed it into a mex-file. now i want the results from this to be handed over to matlab. i wasn't sure what the best way would be. so i just tried to create an output matrix which i can then call from matlab.

  double *outArray;
  plhs[0] = mxCreateDoubleMatrix(8760,3, mxREAL);
  outArray = mxGetPr(plhs[0]);

so as you see the output matrix should have 8760 rows and 3 columns.
after the calculations are done i want to put the different variables in that matrix. i tried it like this:

for(i=0;i<=8759;i++)
{
outArray[1][i]=calculations1[i];
outArray[2][i]=calculations2[i];
outArray[3][i]=calculations3[i];
}

i always get the message: error: invalid types `double[int]' for array subscript

how can i access the different columns of the matrix i created?
would be great if someone could help me out!

thanks in advance,

jan

Subject: index of matrix in mex-file

From: madapeti@gmail.com

Date: 26 Apr, 2009 19:35:06

Message: 2 of 4

Dear Jan,

Although I am not an expert in the field, I suppose, that as mxGetPr
returns a double * you may not be able to use it, as you originally
intended.
Instead you could calculate the index values for the 1D double array
e.g.:

If A is an M x N (row, column) matrix, then the (i,j) th element of it
(i : 0..M-1 , j: 0..N-1) in the C array representation will have the
index: j*M + i
This should be true, as MATLAB stores its matrices in a columnwise
order.

So in your case, you should use something like:

outArray[i * 8760 + 0] =3D calculations1[i];
outArray[i * 8760 + 1] =3D calculations2[i];
outArray[i * 8760 + 2] =3D calculations3[i];

(i is the second index for the matrix)

I hope I was correct, and it did help you to solve your problem.

Peter

On Apr 26, 2:22=A0pm, "Jan Widmann" <jtauc...@hotmail.com> wrote:
> hi all,
>
> probably this is one stupid question, but i couldn't find a solution all =
day long...
>
> i had a c/c++ code and changed it into a mex-file. now i want the results=
 from this to be handed over to matlab. i wasn't sure what the best way wou=
ld be. so i just tried to create an output matrix which i can then call fro=
m matlab.
>
> =A0 double *outArray;
> =A0 plhs[0] =3D mxCreateDoubleMatrix(8760,3, mxREAL);
> =A0 outArray =3D mxGetPr(plhs[0]);
>
> so as you see the output matrix should have 8760 rows and 3 columns.
> after the calculations are done i want to put the different variables in =
that matrix. i tried it like this:
>
> for(i=3D0;i<=3D8759;i++)
> {
> outArray[1][i]=3Dcalculations1[i];
> outArray[2][i]=3Dcalculations2[i];
> outArray[3][i]=3Dcalculations3[i];
>
> }
>
> i always get the message: error: invalid types `double[int]' for array su=
bscript
>
> how can i access the different columns of the matrix i created?
> would be great if someone could help me out!
>
> thanks in advance,
>
> jan

Subject: index of matrix in mex-file

From: Jan Widmann

Date: 27 Apr, 2009 08:13:02

Message: 3 of 4


thanks for you answer. I tried it your way, but as it looks to me it doesn't put the data in a matrix that way (not in different columns). Instead it just writes all the data in one long column... Is there a trick to this or have I forgotten something maybe??

Subject: index of matrix in mex-file

From: James Tursa

Date: 27 Apr, 2009 21:03:01

Message: 4 of 4

"Jan Widmann" <jtaucher@hotmail.com> wrote in message <gt28o9$gpj$1@fred.mathworks.com>...
>
> how can i access the different columns of the matrix i created?
> would be great if someone could help me out!
>
> thanks in advance,
>
> jan

MATLAB matrix memory is column-ordered, meaning that columns are stored contiguously. But in C the memory is stored in row-ordered fashion, meaning the rows are stored contiguously. So a 2-dimensional matrix in MATLAB, in memory, will look like its transpose on the C side. For your example, you can just do your own single dimension indexing per the first reply you got, or you can set up pointers to do the double indexing (but do it like the transpose on the C side), or you can just use memcpy. I have included all three examples below.

James Tursa

--------------------------------------------------------------------------------------------------------

#include"mex.h"
#define M 8760
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *outArray;
    double calculations1[M], calculations2[M], calculations3[M];
    mwSize i;
    plhs[0] = mxCreateDoubleMatrix(M,3, mxREAL);
    outArray = mxGetPr(plhs[0]);
    for( i=0; i<M; i++ ) { // fill in with sample data
        calculations1[i] = i + 1;
        calculations2[i] = 2*(i + 1);
        calculations3[i] = 3*(i + 1);
    }
    for( i=0; i<M; i++ ) { // Access the output as a 1-dim array
        outArray[i] = calculations1[i];
        outArray[i+M] = calculations2[i];
        outArray[i+2*M] = calculations3[i];
    }
}

----------------------------------------------------------------------------------------------------------

#include "mex.h"
#define M 8760
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *outArray[3]; // Note that outArray is an array of double *
    double calculations1[M], calculations2[M], calculations3[M];
    mwSize i;
    plhs[0] = mxCreateDoubleMatrix(M,3, mxREAL);
    outArray[0] = mxGetPr(plhs[0]);
    outArray[1] = outArray[0] + M;
    outArray[2] = outArray[1] + M;
    for( i=0; i<M; i++ ) { // fill in with sample data
        calculations1[i] = i + 1;
        calculations2[i] = 2*(i + 1);
        calculations3[i] = 3*(i + 1);
    }
    for( i=0; i<M; i++ ) { // C indexing is like the tranpose of MATLAB variable
        outArray[0][i] = calculations1[i];
        outArray[1][i] = calculations2[i];
        outArray[2][i] = calculations3[i];
    }
}

---------------------------------------------------------------------------------------------------------

#include <string.h>
#include"mex.h"
#define M 8760
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *outArray;
    double calculations1[M], calculations2[M], calculations3[M];
    mwSize i;
    plhs[0] = mxCreateDoubleMatrix(M,3, mxREAL);
    outArray = mxGetPr(plhs[0]);
    for( i=0; i<M; i++ ) { // fill in with sample data
        calculations1[i] = i + 1;
        calculations2[i] = 2*(i + 1);
        calculations3[i] = 3*(i + 1);
    }
    memcpy(outArray, calculations1, M*sizeof(double));
    memcpy(outArray+M, calculations2, M*sizeof(double));
    memcpy(outArray+2*M, calculations3, M*sizeof(double));
}

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us