一是 如何在matlab里调用其他语言写的代码。 (见例子:使用C-MEX技术,ActiveX技术)
二是 如何在其他语言里调用matlab。 (见 使用matlab引擎, MAT数据交换, matlab发布com组件, DeployTool)
1. 数据导入导出,主要是MAT文件数据的导入导出。
2.普通的动态链接库dll文件的交互,Matlab6.5起,直接matlab环境中调用dll文件导出的函数。
3.matlab环境中调用c/c++语言代码的接口,通过MEX(MATLAB EXecutable)技术实现。C/C++代码通过实现特殊的入口函数,编译成MEX文件(实际dll),可以像一般的m文件被调用,调用级别比M函数高。
4.matlab环境中调用java,6.0版本起,matlab增加java支持,其工作界面GUI也是java编写的,且包含了Java虚拟机。
5.对COM与DDE的支持,5.1后DDE没再增加新内容,6.5后推荐COM接口。matlab的com编译器能将matlab函数转换成COM对象供多种语言使用。
6.matlab使用网络服务或者和串口通讯。
前段客户端使用 matlab引擎(matlab的一组接口函数engXXXX) 与后台启动的matlab进程 进行数据交换和命令的传送。
几乎可以利用matlab的全部功能,但需要计算机上安装matlab软件,执行效率低,不宜用于商业应用软件。
VC++ 开发环境,需要添加Include: {matlab安装目录}\extern\include\ Library Files: {matlab安装目录}\extern\lib\win32\microsoft\
C and Fortran Engine Library Functions
| engOpen (C and Fortran) | Start MATLAB engine session |
| engOpenSingleUse (C) | Start MATLAB engine session for single, nonshared use |
| engClose (C and Fortran) | Quit MATLAB engine session |
| Engine (C) | Type for MATLAB engine |
| engEvalString (C and Fortran) | Evaluate expression in string |
| engGetVariable (C and Fortran) | Copy variable from MATLAB engine workspace |
| engPutVariable (C and Fortran) | Put variable into MATLAB engine workspace |
| engGetVisible (C) | Determine visibility of MATLAB engine session |
| engSetVisible (C) | Show or hide MATLAB engine session |
| engOutputBuffer (C and Fortran) | Specify buffer for MATLAB output |
engPutArray 和 engGetArray 已过时,被engPutVariable 和 engGetVariable取替,但仍可使用。
<pre name="code" class="cpp"> mxArray * T = mxCreateDoubleMatrix(1, 10, mxREAL); memcpy((char *) mxGetPr(T), (char *) time, 10*sizeof(double)); /* * Place the variable T into the MATLAB workspace */ engPutVariable(ep, "T", T); /* * Evaluate a function of time, distance = (1/2)g.*t.^2 * (g is the acceleration due to gravity) */ engEvalString(ep, "D = .5.*(-9.8).*T.^2;"); /* * Use engOutputBuffer to capture MATLAB output. Ensure first that * the buffer is always NULL terminated. */ buffer[BUFSIZE] = '\0'; engOutputBuffer(ep, buffer, BUFSIZE); /* * the evaluate string returns the result into the * output buffer. */ engEvalString(ep, "whos"); MessageBox ((HWND)NULL, (LPSTR)buffer, (LPSTR) "MATLAB - whos", MB_OK);
完整项目:demo1
C and Fortran MAT-File Library Functions
| matOpen (C and Fortran) | Open MAT-file |
| matClose (C and Fortran) | Close MAT-file |
| MATFile (C and Fortran) | Type for MAT-file |
| matGetVariable (C and Fortran) | Array from MAT-file |
| matGetVariableInfo (C and Fortran) | Array header information only |
| matGetNextVariable (C and Fortran) | Next array in MAT-file |
| matGetNextVariableInfo (C and Fortran) | Array header information only |
| matPutVariable (C and Fortran) | Array to MAT-file |
| matPutVariableAsGlobal (C and Fortran) | Array to MAT-file as originating from global workspace |
| matDeleteVariable (C and Fortran) | Delete array from MAT-file |
| matGetDir (C and Fortran) | List of variables in MAT-file |
| matGetFp (C) | File pointer to MAT-file |
| mxIsFromGlobalWS (C and Fortran) | Determine whether array was copied from MATLAB global workspace |
pmat = matOpen(file,"r");
if (NULL == pmat){
sprintf(buf, "打开文件错误 %s",file);
MessageBox(buf);
return;
}
m_list.AddString("打开文件");
/** read in each array we just wrote **/
pa1 = matGetVariable(pmat,"LocalDouble");
if(NULL == pa1){
MessageBox("读取 matrix LocalDouble failed");
return;
}
m_list.AddString("读取matix LocalDouble");
if(mxGetNumberOfDimensions(pa1) != 2){
MessageBox("矩阵保存错误:结果不是两维!");
return;
}
m_list.AddString("矩阵是两维的");
pa2 = matGetVariable(pmat,"GlobalDouble");
if(NULL == pa2){
MessageBox("读取 matrix GlobalDouble failed");
return;
}
m_list.AddString("读取GlobalDouble");
if( !(mxIsFromGlobalWS(pa2)) ) {
MessageBox("全局保存错误,GlobalDouble不是全局的");
}
m_list.AddString("GlobalDouble是全局的");
pa3 = matGetVariable(pmat,"LocalString");
if(NULL == pa3){
MessageBox("读取LocalString错误");
return;
}
m_list.AddString("读取LocalString");
status = mxGetString(pa3, str, sizeof(str));
if( 0 != status){
MessageBox("空间不足");
return;
}
if( strcmp(str,"MATLAB: the language of technical computing") ){
MessageBox("字符串保存错误");
}
m_list.AddString("字符串保存完整");
/** clean up before exit **/
mxDestroyArray(pa1);
mxDestroyArray(pa2);
mxDestroyArray(pa3);
if(matClose(pmat) != 0){
sprintf(buf, "",file);
MessageBox(buf);
return;
}
m_list.AddString("关闭文件");完整项目:demo2C MEX Library Functions
| mexFunction | Entry point to C/C++ or Fortran MEX file |
| mexFunctionName | Name of current MEX function |
| mexAtExit | Register function to call when MEX function clears or MATLAB terminates |
| mexCallMATLAB | Call MATLAB function, user-defined function, or MEX file |
| mexCallMATLABWithTrap | Call MATLAB function, user-defined function, or MEX-file and capture error information |
| mexEvalString | Execute MATLAB command in caller workspace |
| mexEvalStringWithTrap | Execute MATLAB command in caller workspace and capture error information |
| mexGet | Value of specified graphics property |
| mexSet | Set value of specified graphics property |
| mexGetVariable | Copy of variable from specified workspace |
| mexGetVariablePtr | Read-only pointer to variable from another workspace |
| mexPutVariable | Array from MEX-function into specified workspace |
| mexPrintf | ANSI C PRINTF-style output routine |
| mexSetTrapFlag | Control response of MEXCALLMATLAB to errors |
| mexErrMsgIdAndTxt | Display error message with identifier and return to MATLAB prompt |
| mexWarnMsgIdAndTxt | Warning message with identifier |
| mexErrMsgTxt | Display error message and return to MATLAB prompt |
| mexWarnMsgTxt | Warning message |
| mexIsLocked | Determine if MEX-file is locked |
| mexLock | Prevent clearing MEX-file from memory |
| mexUnlock | Allow clearing MEX-file from memory |
| mexMakeArrayPersistent | Make array persist after MEX file completes |
| mexMakeMemoryPersistent | Make memory allocated by MATLAB software persist after MEX-function completes |
C Syntax
#include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Fortran Syntax
#include "fintrf.h" subroutine mexFunction(nlhs, plhs, nrhs, prhs) integer nlhs, nrhs mwPointer plhs(*), prhs(*)
Arguments
nlhs
Number of expected output mxArrays
plhs
Array of pointers to the expected output mxArrays
nrhs
Number of input mxArrays
prhs
Array of pointers to the input mxArrays. Do not modify any prhs values
in your MEX file.
</pre><pre code_snippet_id="1704112" snippet_file_name="blog_20160601_4_4428364" name="code" class="cpp"><pre name="code" class="cpp">/*=================================================================
* mexfunction.c
*
* This example demonstrates how to use mexFunction. It returns
* the number of elements for each input argument, providing the
* function is called with the same number of output arguments
* as input arguments.
* This is a MEX-file for MATLAB.
* Copyright 1984-2006 The MathWorks, Inc.
* All rights reserved.
*=================================================================*/
/* $Revision: 1.5.6.2 $ */
#include "mex.h"
#pragma comment(lib,"libmx.lib")
//#pragma comment(lib,"libmat.lib")
#pragma comment(lib,"libmex.lib")
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
int i;
/* Examine input (right-hand-side) arguments. */
mexPrintf("\nThere are %d right-hand-side argument(s).", nrhs);
for (i=0; i<nrhs; i++) {
mexPrintf("\n\tInput Arg %i is of type:\t%s ",i,mxGetClassName(prhs[i]));
}
/* Examine output (left-hand-side) arguments. */
mexPrintf("\n\nThere are %d left-hand-side argument(s).\n", nlhs);
if (nlhs > nrhs)
mexErrMsgTxt("Cannot specify more outputs than inputs.\n");
for (i=0; i<nlhs; i++) {
plhs[i]=mxCreateDoubleMatrix(1,1,mxREAL);
*mxGetPr(plhs[i])=(double)mxGetNumberOfElements(prhs[i]);
}
}VARIANT x,y,out1; HRESULT hr = CoInitialize(NULL); ICOMDemo1 *pImycomponentclass; hr = CoCreateInstance( CLSID_COMDemo1, NULL, CLSCTX_INPROC_SERVER, IID_ICOMDemo1, (void **)& pImycomponentclass); // x.vt = VT_R8; y.vt = VT_R8; x.dblVal = m_mul1; y.dblVal = m_mul2; // hr = (pImycomponentclass -> multidouble(1, &out1, x, y)); m_result = out1.dblVal; // CoUninitialize();完整项目: demo3
HRESULT hr = CoInitialize(NULL);
VARIANT x, y, out1;
try{
ICOMDemo1Ptr pImycomponentclass;
hr = pImycomponentclass.CreateInstance(OLESTR("COMDemo1.COMDemo1.1_0"));
if(FAILED(hr)) _com_issue_error(hr);
x.vt = VT_R8;
y.vt = VT_R8;
x.dblVal = m_mul1;
y.dblVal = m_mul2;
hr = (pImycomponentclass -> multidouble(1,&out1,x,y));
m_result = out1.dblVal;
}catch(_com_error & e)
{
MessageBox( e.Description().length() != 0 ? (LPCSTR) e.Description(): e.ErrorMessage());
}
//
CoUninitialize();完整项目:demo4 //
VARIANT x, y, out1;
//
HRESULT hr = CoInitialize(NULL);
//
XYDispDriver disp;
bool res = disp.CreateObject("COMDemo1.COMDemo1");
if (false == res) return;
x.vt = VT_R8;
y.vt = VT_R8;
x.dblVal = m_mul1;
y.dblVal = m_mul2;
//
disp.InvokeMethod("multidouble",1, &out1, x, y);
m_result = out1.dblVal ;
//
CoUninitialize();完整项目:http://download.csdn.net/detail/fonjames/9537997
C/C++ API
| mclmcrInitialize | Initializes the MATLAB Runtime proxy library |
| mclInitializeApplication | Set up application state shared by all MATLAB Runtime instances created in current process |
| mclTerminateApplication | Close MATLAB Runtime-internal application state |
| <library>Initialize[WithHandlers] | Initialize MATLAB Runtime instance associated with library |
| <library>Terminate | Free all resources allocated by MATLAB Runtime instance associated with library |
| mwArray | Class used to pass input/output arguments to C functions generated by MATLAB Compiler SDK |
| mwException | Exception type used by the mwArray API and the C++ interface functions |
| mwString | String class used by the mwArray API to pass string data as output from certain methods |
bool res = false;
res = mclInitializeApplication(NULL,0);
if(!res) MessageBox("初始化Application错误!");
res = libmccdemo1Initialize();
if(!res) MessageBox("初始化Lib错误!");
---
mccdemo1();
mclWaitForFiguresToDie(NULL);
---
res = mclTerminateApplication();
if(!res) MessageBox("结束程序错误!");
libmccdemo1Terminate(); // TODO: Add extra initialization here
bool res = false;
res = mclInitializeApplication(NULL,0);
if(!res) MessageBox("初始化Application错误");
res = libmccdemo2Initialize();
if(!res) MessageBox("初始化Lib错误!");
------
// TODO: Add your control notification handler code here
double figsize[] = {0.0 , 0.0};
//
CRect client_rect;
GetClientRect(&client_rect);
figsize[0] = client_rect.Width();
figsize[1] = client_rect.Height();
//
mwArray mwFigSize(2,1, mxDOUBLE_CLASS, mxREAL);
mwFigSize.SetData(figsize,2);
//call fun
mccdemo2(mwFigSize);
//
m_figure.PasteFigure();
----
// TODO: Add your message handler code here
bool res = mclTerminateApplication();
if (!res) MessageBox ("结束程序错误");
libmccdemo2Terminate(); // TODO: Add extra initialization here
bool res = false;
res = mclInitializeApplication(NULL,0);
if (!res) MessageBox("初始化Application错误!");
res = libmccdemo3Initialize();
if (!res) MessageBox("初始化Lib错误!");
-----
char wnd_name[] = "MyTest";
mwArray mwWndName(wnd_name);
mccdemo3(mwWndName);
HWND hFig = ::FindWindow(NULL,wnd_name);
while( hFig == NULL) hFig = ::FindWindow(NULL,wnd_name);
::ShowWindow(hFig, SW_HIDE);
CRect figure_rt;
CWnd *myfigure = GetDlgItem(IDC_STATIC);
myfigure->GetWindowRect(&figure_rt);
long fig_w = figure_rt.Width();
long fig_h = figure_rt.Height();
::SetParent(hFig,myfigure->GetSafeHwnd());
long lStyle = ::GetWindowLong(hFig,GWL_STYLE);
::SetWindowLong(hFig, GWL_STYLE,lStyle &(~ WS_CAPTION) & (~ WS_THICKFRAME));
::ShowWindow(hFig, SW_SHOW);
::SetWindowPos(hFig, NULL, 0,0, fig_w+8, fig_h +33, SWP_NOZORDER|SWP_NOACTIVATE);
::ShowWindow(hFig, SW_SHOW);
::SetForegroundWindow(this -> m_hWnd);
----
bool res = mclTerminateApplication();
if(!res) MessageBox("Terminate failed");
libmccdemo3Terminate();
HRESULT hr;
wchar_t progid[] = L"Matlab.Application";
CLSID clsid;
::CLSIDFromProgID(progid, &clsid);
IDispatch* pIDdispatch = NULL;
::CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pIDdispatch);
DISPID dispid;
OLECHAR *name = L"Execute";
pIDdispatch -> GetIDsOfNames(IID_NULL, &name, 1 , GetUserDefaultLCID(), &dispid);
DISPPARAMS dispparams = { NULL, NULL, 1, 0};
VARIANT para;
para.vt = VT_BSTR;
para.bstrVal = A2BSTR(m_Command.GetBuffer(512));
VARIANT result;
dispparams.rgvarg = ¶
hr = pIDdispatch ->Invoke(dispid, //DISPID
IID_NULL, //must be iid_null
GetUserDefaultLCID(), DISPATCH_METHOD, //method
&dispparams, //methods arguments
&result, //results
NULL, //exception
NULL);//arguments error
m_result = result.bstrVal;
Use the mxCreate* functions to create MATLAB? arrays.
Use the mxCalloc, mxMalloc,
and mxRealloc functions to allocate dynamic memory.
You allocate memory whenever you use an mxCreate* function or when you call the mxCalloc and
associated functions. Use mxDestroyArray to free memory allocated by the mxCreate* functions.
Use mxFree to free memory allocated by the mxCalloc and
associated functions.
| mxCreateDoubleMatrix | 2-D, double-precision, floating-point array |
| mxCreateDoubleScalar | Scalar, double-precision array initialized to specified value |
| mxCreateNumericMatrix | 2-D numeric matrix |
| mxCreateNumericArray | N-D numeric array |
| mxCreateUninitNumericMatrix | Uninitialized 2-D numeric matrix |
| mxCreateUninitNumericArray | Uninitialized N-D numeric array |
| mxCreateString | 1-N array initialized to specified string |
| mxCreateCharMatrixFromStrings | 2-D mxChar array initialized to specified value |
| mxCreateCharArray | N-D mxChar array |
| mxCreateLogicalScalar | Scalar, logical array |
| mxCreateLogicalMatrix | 2-D logical array |
| mxCreateLogicalArray | N-D logical array |
| mxCreateSparseLogicalMatrix | 2-D, sparse, logical array |
| mxCreateSparse | 2-D sparse array |
| mxCreateSparseLogicalMatrix | 2-D, sparse, logical array |
| mxCreateStructMatrix | 2-D structure array |
| mxCreateStructArray | N-D structure array |
| mxCreateCellMatrix | 2-D cell array |
| mxCreateCellArray | N-D cell array |
| mxDestroyArray | Free dynamic memory allocated by MXCREATE* functions |
| mxDuplicateArray | Make deep copy of array |
| mxCalloc | Allocate dynamic memory for array, initialized to 0, using MATLAB memory manager |
| mxMalloc | Allocate uninitialized dynamic memory using MATLAB memory manager |
| mxRealloc | Reallocate dynamic memory using MATLAB memory manager |
| mxFree | Free dynamic memory allocated by mxCalloc, mxMalloc, mxRealloc, mxArrayToString, or mxArrayToUTF8String functions |
原文:http://blog.csdn.net/fonjames/article/details/51554385