Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Returning multiple matrices from a DLL
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



Report this thread to moderator Post Follow-up to this message
Old Post
Alain Sienaert
05-30-05 09:01 PM


Re: Returning multiple matrices from a DLL
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

Report this thread to moderator Post Follow-up to this message
Old Post
Stefan
05-30-05 09:01 PM


Re: Returning multiple matrices from a DLL
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

Report this thread to moderator Post Follow-up to this message
Old Post
Alain Sienaert
05-30-05 09:01 PM


Re: Returning multiple matrices from a DLL
Hi Alain Sienaert,

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

Regards,
Stefan

Report this thread to moderator Post Follow-up to this message
Old Post
StefAN
05-31-05 01:59 AM


Re: Returning multiple matrices from a DLL
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Steven Lord
05-31-05 09:05 PM


Re: Returning multiple matrices from a DLL
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;
}


Report this thread to moderator Post Follow-up to this message
Old Post
Chris Hulbert
05-31-05 09:05 PM


Re: Returning multiple matrices from a DLL
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
> 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
>
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Alain Sienaert
06-01-05 02:00 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Matlab archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:38 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.