Thread Subject: Matlab c++ compiled dll: large input array/string

Subject: Matlab c++ compiled dll: large input array/string

From: MatlabFCT NG

Date: 20 Nov, 2009 23:51:35

Message: 1 of 5

Hello,

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 Question: 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 a lot! Any hint will be very helpful :).

Subject: Matlab c++ compiled dll: large input array/string

From: Bruno Luong

Date: 21 Nov, 2009 06:56:13

Message: 2 of 5

MatlabFCT NG <matfctng@gmail.com> wrote in message <cabe1e40-6bd5-4096-9839-9de1d24c4db9@o10g2000yqa.googlegroups.com>...

>
> First Question: 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....

Something is not right in your example. Inside Matlab it should be

| 1 ...
  2 ...
  3 ...
  4 ...
  5 ... |

Because Matlab data organized column wise.

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

Use mxCreateString(...)

Bruno

Subject: Matlab c++ compiled dll: large input array/string

From: MatlabFCT NG

Date: 21 Nov, 2009 16:23:25

Message: 3 of 5

Hi Bruno,

thanks for your answer :). I really don't know what's happening :(..
I follow the example for C libray (Matlab help). The only difference
is they have a square matrix... Maybe I'm not allocating properly my
matrix (Cdata = mxCreateDoubleMatrix(500,4,mxREAL);)...

About my char array, i try the following code:

char *path;
path= "C:\dataopen"
int ndim = 2;
int dims[2];
dims[0] = 1;
dims[1] = 12;

path_C = mxCreateCharArray(ndim, (const mwSize *)&dims);
memcpy(mxGetPr(path_C),path, 12*sizeof(char));

Althougth the data is no being read correctly to matlab. Is this the
right way to do to a char variable?

Thanks for you help :)


On 21 Nov, 06:56, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> MatlabFCT NG <matfc...@gmail.com> wrote in message <cabe1e40-6bd5-4096-9839-9de1d24c4...@o10g2000yqa.googlegroups.com>...
>
> > First Question: 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....
>
> Something is not right in your example. Inside Matlab it should be
>
> | 1 ...
>   2 ...
>   3 ...
>   4 ...
>   5 ... |
>
> Because Matlab data organized column wise.
>
>
>
> > Second question: How can I transfer a char array from C++ to the DLL?
>
> Use mxCreateString(...)
>
> Bruno

Subject: Matlab c++ compiled dll: large input array/string

From: MatlabFCT NG

Date: 21 Nov, 2009 17:36:08

Message: 4 of 5

Hi,

following the suggestion of Bruno I found the solution to my 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

mxDestroyArray(path_C );


On 21 Nov, 16:23, MatlabFCT NG <matfc...@gmail.com> wrote:
> Hi Bruno,
>
> thanks for your answer :). I really don't know what's happening :(..
> I follow the example for C libray (Matlab help). The only difference
> is they have a square matrix... Maybe I'm not allocating properly my
> matrix (Cdata = mxCreateDoubleMatrix(500,4,mxREAL);)...
>
> About my char array, i try the following code:
>
> char *path;
> path= "C:\dataopen"
>         int ndim = 2;
>         int dims[2];
>         dims[0] = 1;
>         dims[1] = 12;
>
>         path_C = mxCreateCharArray(ndim, (const mwSize *)&dims);
> memcpy(mxGetPr(path_C),path, 12*sizeof(char));
>
> Althougth the data is no being read correctly to matlab. Is this the
> right way to do to a char variable?
>
> Thanks for you help :)
>
> On 21 Nov, 06:56, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
>
> > MatlabFCT NG <matfc...@gmail.com> wrote in message <cabe1e40-6bd5-4096-9839-9de1d24c4...@o10g2000yqa.googlegroups.com>...
>
> > > First Question: 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....
>
> > Something is not right in your example. Inside Matlab it should be
>
> > | 1 ...
> >   2 ...
> >   3 ...
> >   4 ...
> >   5 ... |
>
> > Because Matlab data organized column wise.
>
> > > Second question: How can I transfer a char array from C++ to the DLL?
>
> > Use mxCreateString(...)
>
> > Bruno

Subject: Matlab c++ compiled dll: large input array/string

From: Pedro Manuel

Date: 21 Nov, 2009 20:13:02

Message: 5 of 5

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 :)!


MatlabFCT NG <matfctng@gmail.com> wrote in message <93da80a6-5860-4253-a811-af53a55ec17e@u7g2000yqm.googlegroups.com>...
> Hi,
>
> following the suggestion of Bruno I found the solution to my 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
>
> mxDestroyArray(path_C );
>
>
> On 21 Nov, 16:23, MatlabFCT NG <matfc...@gmail.com> wrote:
> > Hi Bruno,
> >
> > thanks for your answer :). I really don't know what's happening :(..
> > I follow the example for C libray (Matlab help). The only difference
> > is they have a square matrix... Maybe I'm not allocating properly my
> > matrix (Cdata = mxCreateDoubleMatrix(500,4,mxREAL);)...
> >
> > About my char array, i try the following code:
> >
> > char *path;
> > path= "C:\dataopen"
> > ? ? ? ? int ndim = 2;
> > ? ? ? ? int dims[2];
> > ? ? ? ? dims[0] = 1;
> > ? ? ? ? dims[1] = 12;
> >
> > ? ? ? ? path_C = mxCreateCharArray(ndim, (const mwSize *)&dims);
> > memcpy(mxGetPr(path_C),path, 12*sizeof(char));
> >
> > Althougth the data is no being read correctly to matlab. Is this the
> > right way to do to a char variable?
> >
> > Thanks for you help :)
> >
> > On 21 Nov, 06:56, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> >
> > > MatlabFCT NG <matfc...@gmail.com> wrote in message <cabe1e40-6bd5-4096-9839-9de1d24c4...@o10g2000yqa.googlegroups.com>...
> >
> > > > First Question: 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....
> >
> > > Something is not right in your example. Inside Matlab it should be
> >
> > > | 1 ...
> > > ? 2 ...
> > > ? 3 ...
> > > ? 4 ...
> > > ? 5 ... |
> >
> > > Because Matlab data organized column wise.
> >
> > > > Second question: How can I transfer a char array from C++ to the DLL?
> >
> > > Use mxCreateString(...)
> >
> > > Bruno

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
mxarray dll Pedro Manuel 21 Nov, 2009 15:14:10
large matrix Pedro Manuel 21 Nov, 2009 15:14:08
rssFeed for this Thread

Contact us at files@mathworks.com