Thread Subject: Return a Cell Array from C++ DLL

Subject: Return a Cell Array from C++ DLL

From: Jun

Date: 26 Oct, 2009 06:39:02

Message: 1 of 6

hi all,
I'm trying to use a C++ DLL to do some computing, and return a cell array X to the matlab code.
The elements inside the cell array have the same data type but different size.
e.g. Cell array X(2*3)
X={{[]},{[2,3]},{[]};
     {[5]},{[]},{[1]} }
    
I've considered 2 ways:
1. Initialize arrays inside the dll code, use the concept of multilevel pointer, and return a pointer
  problem: you have to set the size of a new array in C++,e.g. A[][5]. However, the size of the array varies, and it is not a constant.

e.g.
double *mydll()
{
  double A[10];
  doube *B[2];
  double **C[1];
.....
.....
.....

return C;
}

2. create an empty cell array in matlab code, and pass a pointer of the cell array to the DLL. DLL updates the values of the elements of the cell array.
  problem: cell array in matlab is actually a collection of pointers, and the adresses of the pointers are not continuous. We cannot use function libpointer to find a single pointer pointing to the cell array.

e.g.
loadlibrary mylib myheader.h
myCellArray=cell(100,200);
myPtr=libpointer('doublePtr',myCellArray); % error msg
calllib('mylib ','myheader.h', myPtr);

newCellArray=get( myPtr,'Value');

unloadlibrary mylib;


Anyone knows how to solve these problems? or have a better way to do it?
Thanks~

Subject: Return a Cell Array from C++ DLL

From: dk

Date: 26 Oct, 2009 08:51:22

Message: 2 of 6


>   problem: cell array in matlab is actually a collection of pointers, and the adresses of the pointers are not continuous.  We cannot use function libpointer to find a single pointer pointing to the cell array.
>


How are you saying that "the adresses of the pointers are not
continuous". Do you have any reference?

Subject: Return a Cell Array from C++ DLL

From: Steven Lord

Date: 26 Oct, 2009 14:08:17

Message: 3 of 6


"Jun " <xujun1023@hotmail.com> wrote in message
news:hc3g66$e1d$1@fred.mathworks.com...
> hi all,
> I'm trying to use a C++ DLL to do some computing, and return a cell array
> X to the matlab code.
> The elements inside the cell array have the same data type but different
> size.
> e.g. Cell array X(2*3)
> X={{[]},{[2,3]},{[]};
> {[5]},{[]},{[1]} }
>
> I've considered 2 ways:

*snip*

The better way is to use the external interface functions to create and fill
the cell array inside your MEX-file/Engine session.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html

http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/bqoqnz0.html

http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mxcreatecellarray.html

http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mxcreatecellmatrix.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Return a Cell Array from C++ DLL

From: Jun

Date: 27 Oct, 2009 01:55:08

Message: 4 of 6

dk <mcdivyakanth@gmail.com> wrote in message <38625f93-1acd-4fc4-95ea-d9ef3a04544c@z34g2000vbl.googlegroups.com>...

> How are you saying that "the adresses of the pointers are not
> continuous". Do you have any reference?

hello dk,

I wrote the adresses of all the elements inside a cell array into a .txt file.

These addresses are independent. I think it's reasonable, since the size of each element is unknown.

Subject: Return a Cell Array from C++ DLL

From: Jun

Date: 27 Oct, 2009 01:59:04

Message: 5 of 6

"Steven Lord" <slord@mathworks.com> wrote in message <hc4af2$s6t$1@fred.mathworks.com>...

> *snip*
>
> The better way is to use the external interface functions to create and fill
> the cell array inside your MEX-file/Engine session.
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/bqoqnz0.html
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mxcreatecellarray.html
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mxcreatecellmatrix.html
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Hi Steve,

thanks for your reply.

But I want my DLL is portable, not only in matlab environment.
mex/engine require installation of matlab.

Subject: Return a Cell Array from C++ DLL

From: James Tursa

Date: 27 Oct, 2009 07:17:00

Message: 6 of 6

"Jun " <xujun1023@hotmail.com> wrote in message <hc5k57$svn$1@fred.mathworks.com>...
> "Steven Lord" <slord@mathworks.com> wrote in message <hc4af2$s6t$1@fred.mathworks.com>...
>
> > *snip*
> >
> > The better way is to use the external interface functions to create and fill
> > the cell array inside your MEX-file/Engine session.
> >
> > http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html
> >
> > http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/bqoqnz0.html
> >
> > http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mxcreatecellarray.html
> >
> > http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mxcreatecellmatrix.html
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> >
>
> Hi Steve,
>
> thanks for your reply.
>
> But I want my DLL is portable, not only in matlab environment.
> mex/engine require installation of matlab.

I don't understand this statement. You want a dll that generates (or at least works with) MATLAB style cell arrays, and yet you *don't* want it to be tied to a MATLAB environment? I don't get it. Can you be more specific here?
James Tursa

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
cell array Jun 26 Oct, 2009 02:47:58
libpointer Jun 26 Oct, 2009 02:47:39
c Jun 26 Oct, 2009 02:44:03
dll Jun 26 Oct, 2009 02:44:03
pointer Jun 26 Oct, 2009 02:44:03
rssFeed for this Thread

Contact us at files@mathworks.com