DSPRelated.com
Forums

Fw: Interfacing Matlab with C++

Started by anser mehboob October 19, 2010
Dear Sumon

Having a quick look on your code I felt that you were also doing your
processing in mexFunction() which may be causing the problem - you may need to
write your desired function saparately in the same file where you
have mexFunction() . Following is the code which I wrote when I did it.
Basically, I needed a function to shift a 16-bit number to the left or right and
there was no inbuilt Matlab function to to do it at that time. So I wrote a
function called bitshift16(number,shift) and interfaced it to mexFunction(). 

 I hope this will help you.

Kind Regards

Anser

#include#include#include"matrix.h"#include"mex.h"#defineIN1
prhs[0]#defineIN2 prhs[1]#defineOUT plhs[0]static
{voidbitshift16(int*temp, intnum, intsign)if
{
*temp=num< }(sign > 0)return; if
{
signs(sign);
*temp=num>>sign;
}(sign < 0)return; else{
*temp=num;
}
}return;  
 
void
{mexFunction(intnlhs,mxArray *plhs[],intnrhs,const mxArray
*prhs[])intin1;intin2;int*out;/* Check for proper number of arguments
*/mexErrMsgTxt(
}
mexErrMsgTxt(
}
mexErrMsgTxt(
}
plhs[0]= mxCreateNumericMatrix(1, 1, mxINT16_CLASS, mxREAL); if(nrhs != 2) {
"Two input argument required."); elseif(nlhs > 1) {"Too many output
arguments."); elseif( !mxIsNumeric(prhs[0])&& !mxIsNumeric(prhs[1]) ) {"Argument
must be numeric.");//mxCreateCellMatrix(1,1); //in1 = (
in2 = (
out = (
bitshift16(out, in1 ,in2);
}int) mxGetScalar(prhs[0]);int) mxGetScalar(prhs[1]);int*) mxGetPr(plhs[0]);
return;

 

----- Forwarded Message ----
From: abdul mannan
To: anser mehboob
Sent: Tue, 19 October, 2010 2:00:22
Subject: Re: [matlab] Interfacing Matlab with C++
Dear Anser
                 Thank you for your reply.
I am trying to use visual C++ codes from Matlab commend window, but
unfortunately I cant do that.

I am using Microsoft Visual Studio 9.0 (2008) and MATLAB 7.10.0 (R2010a)
compiler. For the first time I am trying to do a very simple program. I wrote a
visual C++ program and using 'mex' command on matlab command window I compiled
it. After compilation '.mexw32'(for Matlab R2010a) also created, but when I run
the program it does not work and the Matlab facing sudden crush.

If you have enough time please take a look on the following visual C++ codes and
send me the necessary correction.
With Best Ragards,

Sumon

// Program to rgb to yuv conversion

#include "mex.h"
// Used for MATLAB output
// From mex.h
#ifdef __cplusplus
extern "C" {
#endif
/*
 * mex equivalent to MATLAB's "disp" function
 */
extern int mexPrintf(
    const char *fmt,   /*printf style
format */
    ...          /* any additional
arguments */
    );
#ifdef __cplusplus
}
#endif
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Check inputs
if (nrhs != 1)
    mexErrMsgTxt ("This function requires 1 input.");
// Check outputs
if (nlhs > 1)
    mexErrMsgTxt ("Too many return values were specified.");
// Check input types
if (!mxIsDouble (prhs[0]) || mxIsComplex (prhs[0]))
   mexErrMsgTxt ("The input must be a noncomplex double.\n");
// Get input dimensions
int ndims = mxGetNumberOfDimensions (prhs[0]);
const int *dims = mxGetDimensions (prhs[0]);
if ((ndims != 2 && ndims != 3) || dims[ndims-1] != 3)
    mexErrMsgTxt ("The input must be either N X 3 or M X N X 3");
// Get pointers to inputs and outputs
double *yuv = mxGetPr (plhs[0]);
double *rgb = mxGetPr (prhs[0]);
// Determine how many iterations we need
int total = mxGetNumberOfElements (prhs[0]) / 3;

// Do the conversion
for (int n = 0; n < total; ++n)
{
    yuv[n+0*total] =  0.2122*rgb[n] +
        0.7013*rgb[n+total] + 0.0865*rgb[n+2*total];
    yuv[n+1*total] = -0.1162*rgb[n] -
        0.3838*rgb[n+total] + 0.5000*rgb[n+2*total];
    yuv[n+2*total] =  0.5000*rgb[n] -
        0.4451*rgb[n+total] - 0.0549*rgb[n+2*total];
}
return;
}

________________________________
From: anser mehboob
To: m...
Sent: Tue, October 12, 2010 5:27:08 PM
Subject: Fw: [matlab] Interfacing Matlab with C++

 
Hi sumon
You can use 'external interface'  utility in Matlab to interface c/c++ code to
Matlab. I did it 5 years ago so couldn't remember everything but I am sure its
easy, you will need to do a bit of reading in Matlab help for 'External
Interface'. In the meanwhile, I will search the work I did, if I find anything I
will forward it to you.

Regards
Anser 

----- Forwarded Message ----
From: abdul mannan
To: m...
Sent: Mon, 11 October, 2010 12:10:01
Subject: [matlab] Interfacing Matlab with C++

 
hi
    I have done some work in both matlab and c++.i wanted to know a simple
technique to interface them together. My main codes are in matlab. Hence I want
to use this c++ function (or the output of c++ codes) from my matlab platform.

Sumon