"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 columnordered, meaning that columns are stored contiguously. But in C the memory is stored in rowordered fashion, meaning the rows are stored contiguously. So a 2dimensional 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 1dim 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));
}
