MATLAB exercise program (MATLAB calls C/C ++)

Source: Internet
Author: User

My environment here is window 7 + vs2010 + MATLAB r2010b.

First, you must enter Mex-setup to determine the compiler to be used. Follow the prompts step by step.

The following is the c ++ file. The C ++ file name is the name of the function to be called in the future. Here is sum. cpp.

Sum. cpp:

# Include "Mex. H" // This is required. // The call form Re = sum (arr0, arr1) is used to add the two matrices and assign them to the result matrix. // Nlhs: Number of output parameters // plhs: output parameter list // nrhs: Number of input parameters // prhs: input parameter list void Mexico function (INT nlhs, mxarray * plhs [], int nrhs, const mxarray * prhs []) // equivalent to General Main () {int m0 = mxgetm (prhs [0]); // obtain the number of arr0 rows int N0 = mxgetn (prhs [0]); // obtain the number of arr0 columns double * parr0 = (double *) mxgetpr (prhs [0]); // obtain the arr0 pointer int M1 = mxgetm (prhs [1]); int n1 = mxgetn (prhs [1]); double * parr1 = (double *) mxgetpr (prhs [1]); If (M0! = N0 | M1! = N1) mexerrmsgtxt ("two matrix columns should be equal"); plhs [0] = mxcreatedoublematrix (M0, N0, mxreal); // create a M0 row, n0 matrix 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]; // The two matrices are added to the result matrix one by one }}}

Set sum. place CPP in the current directory and enter Mex sum on the terminal. CPP can generate sum. mexw32, the generated file can be considered as the sum () function, and can be called directly according to the call specification.

Here I will add the two images 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);

Final result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

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.