For Programmers: Free Programming Magazines  


Home > Archive > Matlab > May 2005 > Returning multiple matrices from a DLL









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Returning multiple matrices from a DLL
Alain Sienaert

2005-05-30, 4:01 pm

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


Stefan

2005-05-30, 4:01 pm

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
Alain Sienaert

2005-05-30, 4:01 pm

Stefan 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
StefAN

2005-05-30, 8:59 pm

Hi Alain Sienaert,

if you want a function gives back more than one parameter you have to
use pointers.

Regards,
Stefan
Steven Lord

2005-05-31, 4:05 pm


"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...b_external.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


Chris Hulbert

2005-05-31, 4:05 pm

I 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;
}

Alain Sienaert

2005-05-31, 9:00 pm

Steve,

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[color=darkred]
> of adding
matrix[color=darkred]
> to
> function that
none[color=darkred]
> 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...b_external.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
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com