Thread Subject: how to interface C with matlab?

Subject: how to interface C with matlab?

From: Bandana Mishra

Date: 15 Apr, 2008 11:10:18

Message: 1 of 5

I want to write some part of my program in C language so
as to minimise the computational time..but i am not sure
how to interface C with matlab...Anyone can help me in
this regards?

Subject: how to interface C with matlab?

From: Abel Brown

Date: 15 Apr, 2008 13:14:01

Message: 2 of 5

"Bandana Mishra" <bandanavit@gmail.com> wrote in message
<fu22eq$ir6$1@fred.mathworks.com>...
> I want to write some part of my program in C language so
> as to minimise the computational time..but i am not sure
> how to interface C with matlab...Anyone can help me in
> this regards?


1. it's a real pain in the ass! If you have some sort of
run time error in your c code and run it in matlab then
MATLAB will just crash on you. so be prepared to spend most
of your time restarting matlab.

2. newer versions of MATLAB use JTI whatnot that can really
speed up your code if you deal with all the "mlint" messages
and use the profiler to help you find bottlenecks. So, try
those if you havne't already.

3. everything you need to write C that can be run from
matlab is found here:

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f29502.html&http://www.mathworks.com/cgi-bin/texis/webinator/search?pr=Whole_site&db=MSS&prox=page&rorder=750&rprox=750&rdfreq=500&rwfreq=500&rlead=250&sufs=0&order=r&whole=Whole_site&entire_flag=1&is_summary_on=1&ResultCount=10&query=Using+C+in+matlab&submit=Search

4. An Example:

 Note, the MATLAB code runs in 0.977 seconds for a 40x20600
matrix and the C code runs in about 0.018 seconds for the
same data.


MATLAB CODE:

function HTr = compute_residuals(HT)

%mem_allocation

[N,M] = size(HT);
HTr = zeros(N,M);

% loop through data
for j=1:M
    
    % compute the residual
    HTr(:,j)=HT(:,j)-mean(HT(:,j));
    
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


EQUIVALENT C CODE:


#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[]){
 
     //dimention variables
     int i, j, k, m, n;

     //the n x m matrix of input data
     double *input_data;

     //the 1 x m vector of residuals
     double *output_data;

    if(nrhs!=1) {
        mexErrMsgTxt("One input matrix required.");
    }else if(nlhs>1){
        mexErrMsgTxt("Too many output arguments");
    }

    /* Find the dimensions of the input data */
    m = mxGetM(prhs[0]);
    n = mxGetN(prhs[0]);

    /* Create an mxArray for the output data */
    plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);

    /* Create a pointer to the input data */
    input_data = mxGetPr(prhs[0]);

    /* Create a pointer to the output data */
    output_data = mxGetPr(plhs[0]);


     /* Put data in the output array */
    double sum = 0;
    int indx = 0;
    double epoch_mean = 10;
    double tmp;

    for (i = 0; i < n ; i++, indx++){
        
        //get column sum
        for (j = 0; j < m; j++){
            sum+=input_data[j+indx*m];
        }

        epoch_mean = sum/m;
        
        for(k = 0; k < m; k++){
           output_data[k+indx*m] =
           input_data[k+indx*m]-epoch_mean;

        }
                        
        //reset sum
        sum =0;
    }
}

Subject: how to interface C with matlab?

From: Peter Boettcher

Date: 15 Apr, 2008 19:43:38

Message: 3 of 5

"Abel Brown" <brown.2179@osu.edu> writes:

> 2. newer versions of MATLAB use JTI whatnot that can really
> speed up your code if you deal with all the "mlint" messages
> and use the profiler to help you find bottlenecks. So, try
> those if you havne't already.

[snip]

> 4. An Example:
>
> Note, the MATLAB code runs in 0.977 seconds for a 40x20600
> matrix and the C code runs in about 0.018 seconds for the
> same data.
>
>
> MATLAB CODE:
>
> function HTr = compute_residuals(HT)
>
> %mem_allocation
>
> [N,M] = size(HT);
> HTr = zeros(N,M);
>
> % loop through data
> for j=1:M
>
> % compute the residual
> HTr(:,j)=HT(:,j)-mean(HT(:,j));
>
> end

And an example of further optimizing the above code within MATLAB. Your
code takes .95 seconds on my machine (first run) and .52 seconds
thereafter. Simply pulling out the mean computation produces .048
seconds (first run) and .028 after that.

function HTr = compute_residuals(HT)

[N,M] = size(HT);
HTr = zeros(N,M);
mHT = mean(HT);

% loop through data
for j=1:M
    % compute the residual
    HTr(:,j)=HT(:,j)-mHT(j);
end


And, if you have a fairly recent MATLAB, bsxfun will help:

function HTr = compute_residuals(HT)
HTr = bsxfun(@minus, HT, mean(HT));

This one takes .015 seconds after the first "warmup".

Really, my only point is that optimizations inside MATLAB often do
almost as well as MEX files, for much less the pain.

There's also HTr = detrend(HT, 'constant'), but it doesn't do quite as
well as bsxfun.

-Peter

Subject: how to interface C with matlab?

From: Sebastiaan

Date: 15 Apr, 2008 20:20:19

Message: 4 of 5

Dear Bandana,

I think you should read the section about MEX-files in the
Matlab Help. (Product Help -> MATLAB -> External Interfaces).

Depending on what you want, you gain nothing (see above) or
improvements of a factor >500.

Good luck,
Sebastiaan


"Bandana Mishra" <bandanavit@gmail.com> wrote in message
<fu22eq$ir6$1@fred.mathworks.com>...
> I want to write some part of my program in C language so
> as to minimise the computational time..but i am not sure
> how to interface C with matlab...Anyone can help me in
> this regards?

Subject: how to interface C with matlab?

From: Abel Brown

Date: 18 Apr, 2008 17:30:05

Message: 5 of 5

Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyej96c39x.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "Abel Brown" <brown.2179@osu.edu> writes:
>
> > 2. newer versions of MATLAB use JTI whatnot that can really
> > speed up your code if you deal with all the "mlint" messages
> > and use the profiler to help you find bottlenecks. So, try
> > those if you havne't already.
>
> [snip]
>
> > 4. An Example:
> >
> > Note, the MATLAB code runs in 0.977 seconds for a 40x20600
> > matrix and the C code runs in about 0.018 seconds for the
> > same data.
> >
> >
> > MATLAB CODE:
> >
> > function HTr = compute_residuals(HT)
> >
> > %mem_allocation
> >
> > [N,M] = size(HT);
> > HTr = zeros(N,M);
> >
> > % loop through data
> > for j=1:M
> >
> > % compute the residual
> > HTr(:,j)=HT(:,j)-mean(HT(:,j));
> >
> > end
>
> And an example of further optimizing the above code within
MATLAB. Your
> code takes .95 seconds on my machine (first run) and .52
seconds
> thereafter. Simply pulling out the mean computation
produces .048
> seconds (first run) and .028 after that.
>
> function HTr = compute_residuals(HT)
>
> [N,M] = size(HT);
> HTr = zeros(N,M);
> mHT = mean(HT);
>
> % loop through data
> for j=1:M
> % compute the residual
> HTr(:,j)=HT(:,j)-mHT(j);
> end
>
>
> And, if you have a fairly recent MATLAB, bsxfun will help:
>
> function HTr = compute_residuals(HT)
> HTr = bsxfun(@minus, HT, mean(HT));
>
> This one takes .015 seconds after the first "warmup".
>
> Really, my only point is that optimizations inside MATLAB
often do
> almost as well as MEX files, for much less the pain.
>
> There's also HTr = detrend(HT, 'constant'), but it doesn't
do quite as
> well as bsxfun.
>
> -Peter

wow awesome answer! You rock!

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
c and matlab Bandana Mishra 15 Apr, 2008 07:15:05
rssFeed for this Thread

Contact us at files@mathworks.com