Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, We have written a DLL that includes a simple function mxArray* matrixAdd(mxArray * A, mxArray* B) within this function we create a result matrix, store the result of adding matrix A and B in the result matrix and return the result matrix to Matlab. tmpout = mxCreateDoubleMatrix(m, n, mxReal); .... return tmpout; This all works just fine. However we now want to implement a function that returns two matrices. We have tried all kinds of things but none of them seem to work. We are sing a simple example: e.g. a function that allows us to specify two matrices from Matlab whereby the DLL function returns the transpose of those 2 matrices. or a DLL function without arguments that returns two "magic" matrices. All help is really appreciated, Alain
Post Follow-up to this messageHi Alain Sienaert, in my experience you can use just more than one left hand sided argument (outputs) and working with them. What doesn't work in particular? Regards, Stefan
Post Follow-up to this messageStefan wrote: > > > Hi Alain Sienaert, > > in my experience you can use just more than one left hand sided > argument (outputs) and working with them. > What doesn't work in particular? > > Regards, > Stefan Stefan, I'm trying to find out how you declare your c function in the dll so that you can return more than 1 variable. If we only want to return a single matrix from our c function then it is simple: 1) specify that you want to retun a matrix mxArray* matrixAdd(mxArray * A, mxArray* B) 2) Create the matrix you want to return tmpout = mxCreateDoubleMatrix(m, n, mxReal); 3) Use the return function to actually return the matrix return tmpout; When we have two matrices that we want to return, let's say tmpout1 and tmpout2. How do we do this? tmpout1 = mxCreateDoubleMatrix(m, n, mxReal); tmpout2 = mxCreateDoubleMatrix(m, n, mxReal); ???? I guess that a c-function declaration would be a great help. Kind Regards, Alain
Post Follow-up to this messageHi Alain Sienaert, if you want a function gives back more than one parameter you have to use pointers. Regards, Stefan
Post Follow-up to this message"Alain Sienaert" <asienaert@hotmail.com> wrote in message news:429b163f$0$338$ba620e4c@news.skynet.be... > Hi, > > We have written a DLL that includes a simple function > > mxArray* matrixAdd(mxArray * A, mxArray* B) > > within this function we create a result matrix, store the result of adding > matrix A and B in the result matrix and return the result matrix to > Matlab. > > tmpout = mxCreateDoubleMatrix(m, n, mxReal); > .... > return tmpout; > > > This all works just fine. However we now want to implement a function that > returns two matrices. We have tried all kinds of things but none of them > seem to work. > We are sing a simple example: e.g. a function that allows us to specify > two matrices from Matlab whereby the DLL function returns the transpose of > those 2 matrices. > or a DLL function without arguments that returns two "magic" matrices. If this DLL is a MEX-file, open the External Interfaces manual in the MATLAB documentation. The most recent version of this manual is located here: http://www.mathworks.com/access/hel...br /> nal.html In the chapter titled "Creating C Language MEX-Files", in the "Examples of C MEX-Files" section, there is an example that demonstrates how to pass two or more inputs or outputs. -- Steve Lord slord@mathworks.com
Post Follow-up to this messageI think what you're trying to do requires passing back a double pointer
(an array of pointers), or making the return values part of the
argument list. The C-skeleton could look something like:
double **matrixfunc(double *A,double *B,int M,int N) {
double **matrices;
double *matrix;
matrices = (double**)malloc(N_Matrices*sizeof(doubl
e *));
matrix = matrices[0];
/*matrix = A+B */
matrix = matrices[1];
/* matrix = A-B */
:
:
return matrices;
}
Post Follow-up to this messageSteve,
It seems that I'm getting closer to a solution. I now use the
following m-file in combination with a generic DLL written in Delphi
(no MEX).
loadlibrary matlabdll01 matlabdll01.h alias delphi
a=[1.02, 2.04, 3.06];
[a]=calllib('delphi', 'matrixUpdate', a);
a
unloadlibrary Delphi
The matrixUpdate function in the DLL doubles the individual elements
in matrix a:
a=
2.0400 4.0800 6.1200
However, when I run the above m-file a second time, the values in
matrix a are doubled again:
a=
4.0800 8.1600 12.2400
Is this normal behaviour? I would expect that the line
"a=[1.02, 2.04, 3.06];' re-initializes matrix "a".
I also experience a lot of problems when I try to resize the matrix
in the DLL. Sometimes the DLL crashes, it sometimes comes with
unexpected results and it sometimes works as expected. What is the
best way to resize a matrix (mxArray) in a C-file.
I now use:
function void matrixUpdate(mxArray* a_in)
{
mxSetM(a_in,5)
mxSetN(a_in,5)
}
I think I'm missing something here.
Kind Regards,
Alain
Steven Lord wrote:
>
>
>
> "Alain Sienaert" <asienaert@hotmail.com> wrote in message
> news:429b163f$0$338$ba620e4c@news.skynet.be...
result
> of adding
matrix
> to
> function that
none
> of them
> to specify
> transpose of
> matrices.
>
> If this DLL is a MEX-file, open the External Interfaces manual in
> the MATLAB
> documentation. The most recent version of this manual is located
> here:
>
> <http://www.mathworks.com/access/hel.../>
ternal.html>
>
> In the chapter titled "Creating C Language MEX-Files", in the
> "Examples of C
> MEX-Files" section, there is an example that demonstrates how to
> pass two or
> more inputs or outputs.
>
> --
> Steve Lord
> slord@mathworks.com
>
>
>
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.