我在网上找了很多,版本都太老了,每一个用的了的,奇奇怪怪的错误,在同学的帮助调试下终于运行成功一个,贴出来,希望对大家有帮助,调用方法是把m文件生成dll调用的。
m文件如下:
function [output] = test_add(x,y)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
f=imread('C:/distance.bmp');
imshow(f);
output=x+y;
end
vs2008中代码如下:
#include "stdafx.h"
#include "libtest_add.h"
#include <CString>
#include<iostream>
#include<matrix.h>
//#include"matlab.hpp"
using namespace std;
//#include"matlab.hpp"
#pragma comment(lib,"mclmcrrt.lib")
#pragma comment(lib,"libtest_add.lib")
int _tmain(int argc, _TCHAR* argv[])
{
//mxArray* pArrayIn;
//mxArray* pArrayOut;
// pArrayIn=mxCreateDoubleMatrix(1,1,mxREAL);//mxREAL实数数组没有虚部,创建一个一行两列的实数数组
// *(mxGetPr(pArrayIn))=10;
//*mxGetPr(pArrayIn)=
double a = 6;
double b = 8;
double c;
//double a[]={6};
//double b[]={8};
//CString str;`
mclmcrInitialize_proxy();
mwArray mwA(1, 1, mxDOUBLE_CLASS);//这句话有问题!
mwArray mwB(1, 1, mxDOUBLE_CLASS);
mwArray mwC(1, 1, mxDOUBLE_CLASS);
mwA.SetData(&a, 1);
mwB.SetData(&b, 1);
libtest_addInitialize();
test_add(1, mwC, mwA, mwB);
c = mwC.Get(1, 1);
//CString str;
//str.Format("%.0f",c);
cout<<c<<endl;
//MessageBox(str,"求和结果");
//test_add(1,&pArrayOut,pArrayIn,pArrayIn);
libtest_addTerminate();
return 0;
}