matlab練習程式(matlab調用c/c++)

來源:互聯網
上載者:User

我這裡的環境是window 7+vs2010+matlab R2010b。

首先需要輸入mex -setup來確定需要使用的編譯器,基本按照提示一步步下來就行了。

下面是寫c++檔案,這裡寫的c++檔案名稱就是將來要調用的函數名,我這裡是SUM.cpp

SUM.cpp:

#include "mex.h"    //必須有這個//調用形式 re=SUM(arr0,arr1),將兩個矩陣相加賦值給結果矩陣。//nlhs:輸出參數個數//plhs:輸出參數列表//nrhs:輸入參數個數//prhs:輸入參數列表void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[])    //相當於一般的main()了{    int M0 = mxGetM(prhs[0]);    //得到arr0的行數    int N0 = mxGetN(prhs[0]);    //得到arr0的列數    double* pArr0 = (double*)mxGetPr(prhs[0]);    //得到arr0的指標    int M1 = mxGetM(prhs[1]);    int N1 = mxGetN(prhs[1]);    double* pArr1 = (double*)mxGetPr(prhs[1]);    if(M0!=N0||M1!=N1)        mexErrMsgTxt("兩個矩陣行列應該相等");    plhs[0] = mxCreateDoubleMatrix(M0, N0, mxREAL);    //建立一個M0行,N0列的矩陣    double* pRe =(double*)mxGetPr(plhs[0]);    for(int i=0;i<M0;i++)    {        for (int j=0;j<N0;j++)        {            pRe[i*N0+j]=pArr0[i*N0+j]+pArr1[i*N0+j];    //兩個矩陣逐個相加給結果矩陣        }    }}

將SUM.cpp放入目前的目錄,在終端輸入mex SUM.cpp就能產生SUM.mexw32,產生的這個檔案就可以認為是SUM()函數了,可以直接按調用規範來調用了。

我這裡就將兩個映像相加了main.m:

clear all;close all;clc;a=imread('rice.png');a=double(a);b=imread('cameraman.tif');b=double(b);re=SUM(a,b);re=mat2gray(re);imshow(re);

最後的結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.