Matlab crashes when i create output matrix in mex.
1 view (last 30 days)
Show older comments
Hi, i'm having problems with the creation of the output in a C code:
It runs one time and in the next run Matlab crashes.
I know the problems are in the output generation because i commented the last two for and it runs perfect.
Please i need help!
/*Returning table*/
plhs[0] = mxCreateDoubleMatrix((mwSize)gReadingTableRows,(mwSize)TABLE_COLS,mxREAL);
plhs[1] = mxCreateDoubleMatrix((mwSize)gWritingTableRows,(mwSize)TABLE_COLS,mxREAL);
outReadingTable = mxGetPr(plhs[0]);
outWritingTable = mxGetPr(plhs[1]);
for(i=0;i<gReadingStreamLength;i++){
for(j=0;j<TABLE_COLS;j++){
outReadingTable[gReadingTableRows*i+j]=readingTable[j][i];
}
}
for(i=0;i<gWritingStreamLength;i++){
for(j=0;j<TABLE_COLS;j++){
outWritingTable[gWritingTableRows*i+j]=writingTable[j][i];
}
}
2 Comments
James Tursa
on 22 Feb 2019
Please post all of the relevant code, not just a snippet of where you think the error is. Show us the definitions of gReadingTableRows, gWritingTableRows, TABLE_COLS, readingTable, writingTable, gReadingStreamLength, gWritingStreamLength.
Answers (1)
James Tursa
on 26 Feb 2019
Edited: James Tursa
on 26 Feb 2019
I haven't had much time to look at this in detail, but at first glance this stands out:
void createTable(unsigned int *dataStream, int tableRows, unsigned int dataTable[tableRows][TABLE_COLS])
{
:
int streamLength=sizeof(dataStream)/sizeof(dataStream[0]);
The dataStream variable is a pointer, not an array. So sizeof(dataStream) will be the size of the pointer, not the size of an array. Thus the expression sizeof(dataStream)/sizeof(dataStream[0]) simply calculates sizeof(unsigned int *)/sizeof(unsigned int), which probably works out to 8/4 = 2 if you are runnint 64-bit MATLAB. It does NOT give you the "length" of the data stream behind the pointer. You would need to pass that value into the routine as a separate argument. I.e., you need to pass gReadingStreamLength and gWritingStreamLength as arguments into the createTable function.
Note that this is different from how you do the calculation in mexFunction:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
unsigned int readingStream[20] ={ ... stuff ...};
unsigned int writingStream[30] ={... stuff ...};
:
gReadingStreamLength=sizeof(readingStream)/sizeof(readingStream[0]);
gWritingStreamLength=sizeof(writingStream)/sizeof(writingStream[0]);
Here readingStream and writingStream are arrays, so sizeof(readingStream) and sizeof(writingStream) will in fact be the size of the arrays.
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!