c++调用matlab

1.执行mcc -W lib:liba -T link:lib  oula 生成liba.dll及头文件
2.编译以下c代码,该代码写了一个mxMatDbl类

None.gif //  callMatlab.cpp : Defines the entry point for the console application.
None.gif
//
None.gif

None.gif#include 
" stdafx.h "
None.gif#include 
< windows.h >
None.gif#include 
< matrix.h >
None.gif#include 
" ../liba.h "
None.gif
None.gif
class  mxMatDbl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
InBlock.gif
public:
InBlock.gif    mxArray 
*m_array;
InBlock.gif    mxMatDbl()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        m_array
=0;
ExpandedSubBlockEnd.gif    }

InBlock.gif    mxMatDbl(
int h,int w,const double * pData)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        m_array
=mxCreateDoubleMatrix(h,w, mxREAL); 
InBlock.gif        
if(pData)
InBlock.gif            memcpy(mxGetPr(m_array),pData,h
*w*sizeof(double));
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
double * getPtr()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return mxGetPr(m_array);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
operator double *()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return mxGetPr(m_array);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
operator mxArray *()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return m_array;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
double & operator ()(int y,int x)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        assert(m_array);
InBlock.gif        
int W=w();
InBlock.gif        
int H=h();
InBlock.gif        assert(y
>=0 && y<&& x>=0 && x<W);
InBlock.gif        
return *(mxGetPr(m_array)+ y*+ x);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
int h()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return mxGetM(m_array);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
int w()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return mxGetN(m_array);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
void Release()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(m_array)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            mxDestroyArray(m_array);
InBlock.gif            m_array
=0;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
~mxMatDbl()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Release();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
void Print()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for (int y=0;y<h();y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int x=0;x<w();x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                printf(
"%g ",(*this)(y,x));
ExpandedSubBlockEnd.gif            }

InBlock.gif            printf(
"\n");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
protected:
InBlock.gif
private:
ExpandedBlockEnd.gif}
;
None.gif
None.gif
void  TestMxMatDbl()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
double data[6]=dot.gif{1.0 ,2.0 ,3.0 ,4.0,5.0,6.0};
InBlock.gif    mxMatDbl m(
2,3,data);
InBlock.gif    printf(
"w %d,h %d\n",m.w(),m.h());
InBlock.gif    
for (int y=0;y<m.h();y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for (int x=0;x<m.w();x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            printf(
"%g ",m(y,x));
ExpandedSubBlockEnd.gif        }

InBlock.gif        printf(
"\n");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
void  callMatlab()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    libaInitialize();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
double _r[]=dot.gif{0,0,0};
InBlock.gif    mxMatDbl r(
3,1,_r);
InBlock.gif    mxMatDbl R(
3,3,0);
InBlock.gif    
if(0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//这一局怎么不行??
InBlock.gif
        mxArray *prhs[1];prhs[0]=r.m_array;
InBlock.gif        mxArray 
*plhs[1];plhs[0]=R.m_array;
InBlock.gif        mlxOula(
1, plhs, 1, prhs);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
InBlock.gif        mlxOula(
1&R.m_array, 1&r.m_array);
InBlock.gif    r.Print();
InBlock.gif    R.Print();
InBlock.gif    libaTerminate();
ExpandedBlockEnd.gif}

None.gif
int  main( int  argc,  char *  argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//TestMxMatDbl();
InBlock.gif
    callMatlab();
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值