From: Nilesh on

I have been trying to convert string to integer and store the integer ina structarray.the conversion is fine but when i am using the int mxArray in mxSetField... and putting value ina particualr field it is giving some erraneous output .the integr is supposed to be stored in that field but some string is getting placed in that field and when i am trying it to access the structure array 4-6 time its crashing out with some internal problem.

***********************code************************************
outArray = mxCreateStructMatrix(PQntuples(result),1,PQnfields(result),fname);
for(i=0 ;i< fields;i++)
{
for(j=0; j < tuples; j++)
{
mxArray * mx = mxCreateNumericArray(2,dims,mxINT32_CLASS,mxREAL);
int * pr;
int * temp;
temp = (int *)mxCalloc(1,sizeof(int));
pr = (int *)mxGetData(mx);
*temp = atoi(PQgetvalue(result,j,i));
pr[0] = temp[0];
mexPrintf("%d",pr[0]);
mxSetFieldByNumber(outArray , j,i,mx);
mxDestroyArray(mx);
mxFree(temp);
}
}

here in outarray the integer value is replaced by some string like ' max' ,' tnan trip me'.I am not able to figure out why it is happening
From: James Tursa on
"Nilesh " <nileshjavar(a)gmail.com> wrote in message <i11m30$ks2$1(a)fred.mathworks.com>...
>
> I have been trying to convert string to integer and store the integer ina structarray.the conversion is fine but when i am using the int mxArray in mxSetField... and putting value ina particualr field it is giving some erraneous output .the integr is supposed to be stored in that field but some string is getting placed in that field and when i am trying it to access the structure array 4-6 time its crashing out with some internal problem.
>
> ***********************code************************************
> outArray = mxCreateStructMatrix(PQntuples(result),1,PQnfields(result),fname);
> for(i=0 ;i< fields;i++)
> {
> for(j=0; j < tuples; j++)
> {
> mxArray * mx = mxCreateNumericArray(2,dims,mxINT32_CLASS,mxREAL);
> int * pr;
> int * temp;
> temp = (int *)mxCalloc(1,sizeof(int));
> pr = (int *)mxGetData(mx);
> *temp = atoi(PQgetvalue(result,j,i));
> pr[0] = temp[0];
> mexPrintf("%d",pr[0]);
> mxSetFieldByNumber(outArray , j,i,mx);
> mxDestroyArray(mx);

Don't destroy mx here. You just made it part of your struct matrix outArray, so once you destroy it you corrupt outArray. Get rid of the mxDestroyArray(mx) line.

James Tursa
From: Nilesh on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i11vmi$6gp$1(a)fred.mathworks.com>...
> "Nilesh " <nileshjavar(a)gmail.com> wrote in message <i11m30$ks2$1(a)fred.mathworks.com>...
> >
> > I have been trying to convert string to integer and store the integer ina structarray.the conversion is fine but when i am using the int mxArray in mxSetField... and putting value ina particualr field it is giving some erraneous output .the integr is supposed to be stored in that field but some string is getting placed in that field and when i am trying it to access the structure array 4-6 time its crashing out with some internal problem.
> >
> > ***********************code************************************
> > outArray = mxCreateStructMatrix(PQntuples(result),1,PQnfields(result),fname);
> > for(i=0 ;i< fields;i++)
> > {
> > for(j=0; j < tuples; j++)
> > {
> > mxArray * mx = mxCreateNumericArray(2,dims,mxINT32_CLASS,mxREAL);
> > int * pr;
> > int * temp;
> > temp = (int *)mxCalloc(1,sizeof(int));
> > pr = (int *)mxGetData(mx);
> > *temp = atoi(PQgetvalue(result,j,i));
> > pr[0] = temp[0];
> > mexPrintf("%d",pr[0]);
> > mxSetFieldByNumber(outArray , j,i,mx);
> > mxDestroyArray(mx);
>
> Don't destroy mx here. You just made it part of your struct matrix outArray, so once you destroy it you corrupt outArray. Get rid of the mxDestroyArray(mx) line.
>
> James Tursa


Hi,

Thanks alot for your HELP. iT SOLVED THE PROBLEM .
From: James Tursa on
"Nilesh " <nileshjavar(a)gmail.com> wrote in message <i11m30$ks2$1(a)fred.mathworks.com>...
>
> ***********************code************************************
> outArray = mxCreateStructMatrix(PQntuples(result),1,PQnfields(result),fname);
> for(i=0 ;i< fields;i++)
> {
> for(j=0; j < tuples; j++)
> {
> mxArray * mx = mxCreateNumericArray(2,dims,mxINT32_CLASS,mxREAL);
> int * pr;
> int * temp;
> temp = (int *)mxCalloc(1,sizeof(int));
> pr = (int *)mxGetData(mx);
> *temp = atoi(PQgetvalue(result,j,i));
> pr[0] = temp[0];
> mexPrintf("%d",pr[0]);
> mxSetFieldByNumber(outArray , j,i,mx);
> mxDestroyArray(mx);
> mxFree(temp);
> }
> }

Another comment. What is the point of making temp an int* ? Why not just make it an int and get rid of all that memory allocation stuff associated with it? Or better yet just get rid of it entirely. e.g., this

int * temp;
temp = (int *)mxCalloc(1,sizeof(int));
pr = (int *)mxGetData(mx);
*temp = atoi(PQgetvalue(result,j,i));
pr[0] = temp[0];
mexPrintf("%d",pr[0]);
mxSetFieldByNumber(outArray , j,i,mx);
mxFree(temp);

would become this:

pr = (int *)mxGetData(mx);
pr[0] = atoi(PQgetvalue(result,j,i));
mexPrintf("%d",pr[0]);
mxSetFieldByNumber(outArray , j,i,mx);

Seems simpler and more direct.

James Tursa