I have written an image processing app in Matlab which works fine when
I analyze images one at a time, but is very slow when I have to
analyze thousands of images in a row. So I re-wrote the image analysis
routine in C++, compiled in .NET. The C++ code runs about 20 times
faster than Matlab. Being a layman in programming, I am now wondering
how to make matlab talk to C++.

The C++ code is a .cpp file and an .h file. The .cpp file contains ~5
functions that talk to each other, and the main() produces the final
answer. From what I understand to make a .dll I can export functions
one at a time - is that so? If yes, how can I make main () .dll talk
to the .dlls with the seconday functions and then report to matlab? Or
do I have to bundle all functionalities into the main()?

Or is making a .dll not the right way to go?

Re: running C++ code with mutiple routines from matlab by runcyclexcski

runcyclexcski
Thu Oct 25 20:03:05 PDT 2007

I forgot to add that the arguments to be passed from matlab to the C++
code are two matrices of data type short, about 512 by 512 (two
pointers in C++ notation), and the data returned are also two
matrices.



Re: running C++ code with mutiple routines from matlab by Stuart

Stuart
Thu Oct 25 23:56:05 PDT 2007

runcyclexcski@yahoo.com wrote:

Please don't multi-post. If it can be avoided, cross-post instead (and don't
forget to set up a follow-up tag).

> I have written an image processing app in Matlab which works fine when
> I analyze images one at a time, but is very slow when I have to
> analyze thousands of images in a row. So I re-wrote the image analysis
> routine in C++, compiled in .NET. The C++ code runs about 20 times
> faster than Matlab. Being a layman in programming, I am now wondering
> how to make matlab talk to C++.
>
> The C++ code is a .cpp file and an .h file. The .cpp file contains ~5
> functions that talk to each other, and the main() produces the final
> answer. From what I understand to make a .dll I can export functions
> one at a time - is that so?

Wrong. A Dll can export more than one function. As you are using Visual C, you
can use the tool Dependeny Walker (this tool shipped with Visual C 6.0, I don't
know whether later versions of Visual C still contain this tool) to examine the
functions that are exported by Dlls.

> If yes, how can I make main () .dll talk
> to the .dlls with the seconday functions and then report to matlab? Or
> do I have to bundle all functionalities into the main()?

As the Dll can contain more than one function, is is not necessary.

> Or is making a .dll not the right way to go?

AFAIK, Matlab cannot compile C++ code. Thus making a dll seems to be the only
way to go.

Regards,
Stuart

Re: running C++ code with mutiple routines from matlab by Alex

Alex
Fri Oct 26 01:25:06 PDT 2007

<runcyclexcski@yahoo.com> wrote:
> I have written an image processing app in Matlab which
> works fine when
> I analyze images one at a time, but is very slow when I
> have to
> analyze thousands of images in a row. So I re-wrote the
> image analysis
> routine in C++, compiled in .NET. The C++ code runs about
> 20 times
> faster than Matlab. Being a layman in programming, I am
> now wondering
> how to make matlab talk to C++.

I'm not familiar with Matlab enough to give a definite
answer. However, Matlab can use regular C functions from a
DLL, then you could expose image processing code as plain C
functions. Inside DLL you can implement the functions in
C++.

I have doubts about Matlab understanding C++ functions,
since exported C++ names will be mangled. Only the compiler
that made them will be able to read them back.

You can find comprehensive guide on DLL's here:

"DLLs"
http://msdn2.microsoft.com/en-us/library/1ez7dh12(VS.80).aspx

Alex


Re: running C++ code with mutiple routines from matlab by David

David
Fri Oct 26 07:15:58 PDT 2007

runcyclexcski@yahoo.com wrote in news:1193367785.350572.201340
@q3g2000prf.googlegroups.com:

> I forgot to add that the arguments to be passed from matlab to the C++
> code are two matrices of data type short, about 512 by 512 (two
> pointers in C++ notation), and the data returned are also two
> matrices.

Our program talks to Matlab (ok, Matlab talks to us!). We did it with COM.
(Actually, the COM interface was developed so we could talk to Spotfire,
then we needed to make Matlab work too) The Matlab code calls python and
python talks to COM. We pass the arrays back/forth as SAFEARRAYs. (I'm only
vaguely aware of what the Matlab/python side does, as I work on the COM/C++
side.)

Dave Connet

Re: running C++ code with mutiple routines from matlab by runcyclexcski

runcyclexcski
Fri Oct 26 10:13:36 PDT 2007

Thank you, all, for the responses. Looks like it can be done then. At
sys.matlab they have suggesting using its MEX interface which is based
on .dlls. I was about to start re-writing the whole code in C++ to
begin with.


Re: running C++ code with mutiple routines from matlab by runcyclexcski

runcyclexcski
Fri Oct 26 10:14:50 PDT 2007

Sorry fir the multi-post. I actually did it separately for all 3
groups.