Prev: Neural network - target vector normalization (MLP, newff)
Next: Button Group SelectedObject property
From: Pedro Manuel on 20 Nov 2009 13:10 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!
From: Pedro Manuel on 21 Nov 2009 12:39 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!
From: James Tursa on 21 Nov 2009 13:55 "Pedro Manuel " <matfctng(a)gmail.com> wrote in message <he98jl$m93$1(a)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
From: MatlabFCT NG on 21 Nov 2009 14:54
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_...(a)hotmail.com> wrote: > "Pedro Manuel " <matfc...(a)gmail.com> wrote in message <he98jl$m9...(a)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 |