Thread Subject: Matlab c++ compiled dll: problem mxarray double dim

Subject: Matlab c++ compiled dll: problem mxarray double dim

From: Pedro Manuel

Date: 20 Nov, 2009 18:10:19

Message: 1 of 4

Hi,

I compile my M file into a C++ library, in order to use it in a MSVS C++ project.
One of the inputs of my dll is a large matrix data[500rows*4columns], matrix [10*1] and other is a char array.
(piece of my code to the matrix)
double *data = new double [2000];

mxArray *Cdata;
Cdata = mxCreateDoubleMatrix(500,4,mxREAL);
memcpy(mxGetPr(Cdata),data, 2000*sizeof(double));

First problem: I don't have any problem in the matrix [10*1], however in the large matrix the data is wrongly transfer to the dll matlab, example:

input data: data =
|1 2 3 4 |
|5 6 7 8 |
|9 10 11 12|
|13 14 15 16|
|17 (...) |

inside dll (matlab)
|1 5 9 13|
|17 (...) |

Is there a way to fix this? I would like to avoid performing double transpose in the dll....

Second question: How can I transfer a char array from C++ to the DLL?

Thanks in advance!

Subject: Matlab c++ compiled dll: problem mxarray double dim

From: Pedro Manuel

Date: 21 Nov, 2009 17:39:01

Message: 2 of 4

Hi there,

I found the solution to one of my questions ( char array),

(part of my code)
const char path[] = "c:\\dataopen";
mxArray *path_C;
caminho_C = mxCreateString(path);
memcpy(mxGetPr(path_C),path, sizeof(char));

mlfFoo(1, &y_ptr, path_C);

Unfortunately I don't have any hint regarding the matrix input :(,,,
Can anyone give me some suggestion/hint to follow?

Thank you all


> Hi,
>
> I compile my M file into a C++ library, in order to use it in a MSVS C++ project.
> One of the inputs of my dll is a large matrix data[500rows*4columns], matrix [10*1] and other is a char array.
> (piece of my code to the matrix)
> double *data = new double [2000];
>
> mxArray *Cdata;
> Cdata = mxCreateDoubleMatrix(500,4,mxREAL);
> memcpy(mxGetPr(Cdata),data, 2000*sizeof(double));
>
> First problem: I don't have any problem in the matrix [10*1], however in the large matrix the data is wrongly transfer to the dll matlab, example:
>
> input data: data =
> |1 2 3 4 |
> |5 6 7 8 |
> |9 10 11 12|
> |13 14 15 16|
> |17 (...) |
>
> inside dll (matlab)
> |1 5 9 13|
> |17 (...) |
>
> Is there a way to fix this? I would like to avoid performing double transpose in the dll....
>
> Second question: How can I transfer a char array from C++ to the DLL?
>
> Thanks in advance!

Subject: Matlab c++ compiled dll: problem mxarray double dim

From: James Tursa

Date: 21 Nov, 2009 18:55:07

Message: 3 of 4

"Pedro Manuel " <matfctng@gmail.com> wrote in message <he98jl$m93$1@fred.mathworks.com>...
>
> (part of my code)
> const char path[] = "c:\\dataopen";
> mxArray *path_C;
> caminho_C = mxCreateString(path);
> memcpy(mxGetPr(path_C),path, sizeof(char));

MATLAB stores character strings as 2 bytes per character without any null character at the end, whereas C stores them as 1 byte per character with a null character at the end. You will never get them copied over correctly using memcpy as you have above. You need to use mxCreateString to do that ... it does the whole job. In fact, the memcpy line above only copies one character (incorrectly I might add) in any event. Get rid of that line.

> Unfortunately I don't have any hint regarding the matrix input :(,,,
> Can anyone give me some suggestion/hint to follow?

Impossible to say without seeing all of your matrix code. Is it short enough to post?

James Tursa

Subject: Matlab c++ compiled dll: problem mxarray double dim

From: MatlabFCT NG

Date: 21 Nov, 2009 19:54:26

Message: 4 of 4

Hi James,

the code is this:
(part of my code to char)

const char path[] = "c:\\dataopen";
mxArray *path_C;
path_C = mxCreateString(path); (wrongly copied early from my real
code.. sorry :( )
memcpy(mxGetPr(path_C),path, sizeof(char));

mlfFoo(1, &y_ptr, path_C);

I tested without memcpy....I thought i need memcpy... thanks :D!

Regarding my matrix, I follow the matlab example that they have:

//matrix data
        double *data = new double [2000];
        double *y;

//all my data is read from a file and stored in the matrix "data".
// data will be data=[1 2 3 4 5 6 7 8 ... 2000]


/* Call the MCR and library initialization functions */
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
exit(1);
}

if (!foolibInitialize())
{
fprintf(stderr,"Could not initialize the library.\n");
exit(1);
}

        mxArray *y_ptr=NULL;
        mxArray *Cdata;

//I want to organize my data in 4 columns
       Cdata = mxCreateDoubleMatrix(500,4,mxREAL);
       memcpy(mxGetPr(Cdata),data, 2000*sizeof(double));

//my lib from matlab, I'll perform a bunch of operations with each
column of my data
//it's important to have the 500 1st rows in the 1st column, the 2nds
500 rows
//in the second column, and so on
       mlfFoo(1, &y_ptr, dataC);

foolibTerminate();

mxDestroyArray(y_ptr);
        mxDestroyArray(Cdata);

I don't know if this is enough to you to help me, if you need
something more, please say.

Thanks a lot :)!


On 21 Nov, 18:55, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> "Pedro Manuel " <matfc...@gmail.com> wrote in message <he98jl$m9...@fred.mathworks.com>...
>
>
>
> > (part of my code)
> > const char  path[] = "c:\\dataopen";
> >mxArray*path_C;
> > caminho_C = mxCreateString(path);
> > memcpy(mxGetPr(path_C),path, sizeof(char));
>
> MATLAB stores character strings as 2 bytes per character without any null character at the end, whereas C stores them as 1 byte per character with a null character at the end. You will never get them copied over correctly using memcpy as you have above. You need to use mxCreateString to do that ... it does the whole job. In fact, the memcpy line above only copies one character (incorrectly I might add) in any event. Get rid of that line.
>
> > Unfortunately I don't have any hint regarding the matrix input :(,,,  
> > Can anyone give me some suggestion/hint to follow?
>
> Impossible to say without seeing all of your matrix code. Is it short enough to post?
>
> James Tursa

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
large matrix Pedro Manuel 21 Nov, 2009 12:41:06
mxarray Pedro Manuel 21 Nov, 2009 12:41:05
rssFeed for this Thread

Contact us at files@mathworks.com