Matlab crashes when i create output matrix in mex.

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

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.
Hi, here it is the code!
#include <mex.h>
#include <Windows.h>
#define TABLE_COLS 10
int gReadingStreamLength;
int gWritingStreamLength;
int gReadingTableRows;
int gWritingTableRows;
/**************************************************************************
Internal Functions Prototypes
**************************************************************************/
void createTable(unsigned int *dataStream, int tableRows, unsigned int dataTable[tableRows][TABLE_COLS]);
/*****************************************************************************
** FUNCTION DEFINITIONS
*****************************************************************************/
/**
* \brief Create table of recieved data stream.
*
* \param dataStream Data stream to convert into table
* \param dataTable Data table to write the converted data stream
*
* \return None.
*/
void createTable(unsigned int *dataStream, int tableRows, unsigned int dataTable[tableRows][TABLE_COLS])
{
int i=0, j=0, variablePosition, variableLength;
char variableType[10]; /*Datatype text*/
int streamLength=sizeof(dataStream)/sizeof(dataStream[0]);
mexPrintf("Value: %d||",sizeof(dataStream));
/*Matrix Creation*/
for(j=0;j<20;j++){
dataTable[i][j-(TABLE_COLS*i)]=dataStream[j];
if ((dataStream[j-1]==0x7c)&&(dataStream[j]==0x7c)) i++; /*separate rows*/
}
/* write data type in table*/
for(variablePosition=0;variablePosition<tableRows;variablePosition++){
switch((unsigned int)dataTable[variablePosition][5]){
case 0: strcpy(variableType, "char"); break;
case 1: strcpy(variableType, "uchar"); break;
case 2: strcpy(variableType, "short"); break;
case 3: strcpy(variableType, "ushort"); break;
case 4: strcpy(variableType, "int"); break;
case 5: strcpy(variableType, "uint"); break;
case 6: strcpy(variableType, "long"); break;
case 7: strcpy(variableType, "ulong"); break;
case 8: strcpy(variableType, "int40"); break;
case 9: strcpy(variableType, "uint40"); break;
case 10: strcpy(variableType, "longlong"); break;
case 11: strcpy(variableType, "ulonglong"); break;
case 12: strcpy(variableType, "float"); break;
case 13: strcpy(variableType, "double"); break;
case 14: strcpy(variableType, "longdouble"); break;
case 15: strcpy(variableType, "INVALID"); break;
}
variableLength=dataTable[variablePosition][6]+256*dataTable[variablePosition][7];
/*show table to the user */
mexPrintf(" %d | %c | %d | %s |\n", variablePosition,
dataTable[variablePosition][0], variableLength ,variableType);
}
}
/*****************************************************************************
** FUNCTION DEFINITIONS
*****************************************************************************/
/**
* \brief This function sends a request to read the reading and writing
* table of the data available in the DSP.
*
* \param None.
*
* \return readingTableToShow Requested reading table.
* \return writingTableToShow Requested writing table.
*/
void mexFunction
(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/** Example data stream from serial port*/
unsigned int readingStream[20] ={0x2b,0xe0,0x76,0x00,0x80,0x04,0x01,
0x00,0x7c,0x7c,0x50,0x90,0x6f,0x00,0x80,0x0c,0x80,0x00,0x7c,0x7c};
unsigned int writingStream[30] ={0x4b,0xc8,0x76,0x00,0x80,0x05,0x01,
0x00,0x7c,0x7c,0x41,0xd8,0x76,0x00,0x80,0x0c,0x01,0x00,0x7c,0x7c,
0x45,0xdc,0x76,0x00,0x80,0x0c,0x01,0x00,0x7c,0x7c};
mexPrintf("\n\n Recieved table...\n\n");
/*********************************************************************/
int i=0,j=0;
/*Getting rows and cols of both tables*/
gReadingStreamLength=sizeof(readingStream)/sizeof(readingStream[0]);
gWritingStreamLength=sizeof(writingStream)/sizeof(writingStream[0]);
mexPrintf("Value: %d||",sizeof(writingStream));
gReadingTableRows=gReadingStreamLength/TABLE_COLS;
gWritingTableRows=gWritingStreamLength/TABLE_COLS;
double *outReadingTable; /* output matrix */
double *outWritingTable;
unsigned int readingTable[gReadingTableRows][TABLE_COLS];
unsigned int writingTable[gWritingTableRows][TABLE_COLS];
createTable(readingStream, gReadingTableRows, readingTable);
createTable(writingStream, gWritingTableRows, writingTable);
// /*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];
}
}
}

Sign in to comment.

Answers (1)

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.

Categories

Products

Asked:

on 22 Feb 2019

Edited:

on 26 Feb 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!