Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Calling Matlab from C++ code

Subject: Calling Matlab from C++ code

From: Hisham

Date: 13 Aug, 2007 13:15:52

Message: 1 of 4

Hi,
I want to call my own matlab function from C++ code.
I found a way to do it using COM objects in code project:
http://www.codeproject.com/com/matlabengine.asp

This works for calling standard Matlab functions. What
should I do to call my own functions? where to put the .m
file?
I wanted also to know if the same will work in a computer
where only Matlab component runtime is installed (not
matlab).

p.s If there is another, easier way to do what I need (call
my own matlab functions from C++ and which will work with
MCR) it'll be also good.

Thanks alot

Subject: Calling Matlab from C++ code

From: John reilly

Date: 14 Aug, 2007 11:12:05

Message: 2 of 4

Hi Hisham,

All you need to do to call your own MATLAB m-function is to
have it in the MATLAB path, just like you would have to do
if you were using the MATLAB environment. You can call
addpath() on the COM object to add to the path. It may be
sufficient to just change the cwd of your C++ process.

I doubt you can use the COM object with just the MCR
installed, though I don't know for sure and would like to
find out.

reilly.


"Hisham " <hisham_abusaleh@hotmail.com> wrote in message
<f9pli8$594$1@fred.mathworks.com>...
> Hi,
> I want to call my own matlab function from C++ code.
> I found a way to do it using COM objects in code project:
> http://www.codeproject.com/com/matlabengine.asp
>
> This works for calling standard Matlab functions. What
> should I do to call my own functions? where to put the .m
> file?
> I wanted also to know if the same will work in a computer
> where only Matlab component runtime is installed (not
> matlab).
>
> p.s If there is another, easier way to do what I need
(call
> my own matlab functions from C++ and which will work with
> MCR) it'll be also good.
>
> Thanks alot

Subject: Calling Matlab from C++ code

From: Nasreddine Megrez

Date: 12 Jun, 2008 22:58:01

Message: 3 of 4

Hi,

I am experiencing the same problem. I am not a good C++
programmer and I have to make a plugin using Visual Studio
C++. To avoid C++, I wante to call matlab from C++ to do
my stuff and return the output to C++.

Could you please send me a template code , or one of your
codes where you did the same thing.

Regards




"John Reilly" <jrhokie1.nospam@yahoo.com> wrote in message
<f9s2m5$nri$1@fred.mathworks.com>...
> Hi Hisham,
>
> All you need to do to call your own MATLAB m-function is
to
> have it in the MATLAB path, just like you would have to
do
> if you were using the MATLAB environment. You can call
> addpath() on the COM object to add to the path. It may
be
> sufficient to just change the cwd of your C++ process.
>
> I doubt you can use the COM object with just the MCR
> installed, though I don't know for sure and would like
to
> find out.
>
> reilly.
>
>
> "Hisham " <hisham_abusaleh@hotmail.com> wrote in message
> <f9pli8$594$1@fred.mathworks.com>...
> > Hi,
> > I want to call my own matlab function from C++ code.
> > I found a way to do it using COM objects in code
project:
> > http://www.codeproject.com/com/matlabengine.asp
> >
> > This works for calling standard Matlab functions. What
> > should I do to call my own functions? where to put
the .m
> > file?
> > I wanted also to know if the same will work in a
computer
> > where only Matlab component runtime is installed (not
> > matlab).
> >
> > p.s If there is another, easier way to do what I need
> (call
> > my own matlab functions from C++ and which will work
with
> > MCR) it'll be also good.
> >
> > Thanks alot
>

Subject: Calling Matlab from C++ code

From: Pedro Villena

Date: 13 Jun, 2008 05:09:01

Message: 4 of 4

Hi Hisham,

To call your own matlab function from C++ code, in a
computer without matlab:
1. Instal MCR.
2. compile your m function into exe file or dll file.
3. copy you compiled function in your c++ proyect
directory, and set the path in the environment variables.

This is the best way that recommend mathworks.
Regards,
Pedro

Tags for this Thread

Everyone's Tags:

c, mcr

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
mcr Pedro Villena 13 Jun, 2008 01:10:08
c Ned Gulley 14 Aug, 2007 10:47:45
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics