我们的爱,过了就不再回来 » 日志 » Matlab如何读取C产生的数据
Matlab如何读取C产生的数据
Alan 发表于 2005-03-04 11:00:41
1.最简单的是C的数据写到文件.再由Matlab读文件.
2.Matlab的C/C++数学函数库,有一个mxArray类可以把C++的数据传递给Matlab.不过好像这个库调用不了工具箱的函数,它以一般的矩阵运算为主.
3.自动化接口,通过Matlab.Application或者自定义一个自动化组件.
VC下的OLEView工具可以查看到Matlab.Application组件的接口里面的函数主要
有
GetFullMatrix(...)
PutFullMatrix(...)
Excute(...).
数据的传递是通过自动化的数据类型SAFEARRAY进行.c的数据添加到SafeArray类型的数据就可以作为PutFullFMatrix的参数传递给Matlab处理.
VC下导入Matlab组件和ADO添加类型库预处理类似.
参考一下
www.eas.iis.fhg.de/publications/ slides/2002/015/slides.pdf
A Programming Issue: How to Interface MATLAB with Microsoft Visual C++
1 Introduction
MATLAB® is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.
Most of the research work in current project is implemented under Microsoft Visual C++ (VC++). VC++ is a powerful programming tool for Windows software development. Its programming is relatively low level and can interact with hardware and Operating System directly. Application interface can also be built efficiently under the development environment provided by VC++. Hence high performance software can be easily generated.
However, VC++ is a general programming tool and it does not provide abundant specialized routines as in MATLAB. Many specialized toolboxes coming with MATLAB such as Image Processing, Digital Signal Processing, and Neural Network, reduces much programming effort for researchers to do experiments in these fields. If an interface can be built between MATLAB and VC++, it would be more convenient for us to do the experiments and research work.
2 Possible routes to interface MATLAB with VC++
MATLAB provides interfaces to external routines written in other programming languages, data that needs to be shared with external routines, peripheral devices that communicate directly with MATLAB, and software applications ActiveX and Dynamic Data Exchange. Much of this interface capability was formerly referred to under the title of the MATLAB Application Program Interface, or API. These methods include:
Calling C and Fortran Programs from MATLAB - build C subroutines into callable MEX files.
Calling MATLAB from C Programs - use the MATLAB engine library to call MATLAB from C programs.
Importing and Exporting Data - importing data into and exporting data from the MATLAB environment using MAT-files.
ActiveX and DDE Support - use ActiveX and Dynamic Data Exchange (DDE) with MATLAB.
The ActiveX technique, also usually called OLE Automation in VC++, is the most flexible method among them. ActiveX is a set of object-oriented technologies and tools that allow software developers to integrate application-specific components from different vendors into their own application solution. ActiveX Control components can be both visually and programmatically integrated into an ActiveX control container, such as a MATLAB figure window. ActiveX Automation allows MATLAB to both control and be controlled by other ActiveX components. In the following sections the use of ActiveX to interface MATLAB with VC++ will be introduced in detail.
3 Introduction to ActiveX
ActiveX is a Microsoft Windows protocol for component integration. Using ActiveX, developers and end users can select application-specific, ActiveX components produced by different vendors and seamlessly integrate them into a complete application solution. For example, a single application may require database access, mathematical analysis, and presentation quality business graphs. Using ActiveX, a developer may choose a database access component by one vendor, a business graph component by another, and integrate these into a mathematical analysis package produced by a third.
ActiveX Concepts and Terminology COM
ActiveX is a family of related object-oriented technologies that have a common root, called the Component Object Model, or COM. Each object-oriented language or environment has an object model that defines certain characteristics of objects in that environment, such as how objects are located, instantiated, or identified. COM defines the object model for all ActiveX objects.
ActiveX Interfaces
Each ActiveX object supports one or more named interfaces. An interface is a logically related collection of methods, properties, and events. Methods are similar to function calls in that they are a request for the object to perform some action. Properties are state variables maintained by the object, such as the color of text, or the name of a file on which the control is acting. Events are notifications that the control forwards back to its client.
For example, the sample control shipped with MATLAB has the following methods, properties, and events:
· Methods
Redraw - causes the control to redraw
Beep - causes the control to beep
AboutBox - display the control's "About" dialog
· Properties
Radius (integer) - sets the radius of the circle drawn by the control
Label (string) - text to be drawn in the control
· Events
Click - fired when the user clicks on the control
One important characteristic of COM is that it defines an object model in which objects support multiple interfaces. Some interfaces are standard interfaces, which are defined by Microsoft and are part of ActiveX, and some interfaces are custom interfaces, which are defined by individual component vendors. In order to use any ActiveX object, you must learn about which custom interfaces it supports, and the interface's methods, properties, and events. The ActiveX object's vendor provides this information.
MATLAB supports two ActiveX technologies: ActiveX control containment and ActiveX Automation. ActiveX controls are application components that can be both visually and programmatically integrated into an ActiveX control container, such as MATLAB figure windows. Some examples of useful ActiveX controls are the Microsoft Internet Explorer Web Browser control, the Microsoft Windows Communications control for serial port access, and the graphical user interface controls delivered with the Visual Basic development environment.
ActiveX Automation allows MATLAB to both control and be controlled by other ActiveX components. When MATLAB is controlled by another component, it is acting as an automation server. When MATLAB controls another component, MATLAB is the automation client, and the other component is the automation server.
To utilize the capability of interface development in VC++ and the standard specialized functions in MATLAB the automation server is an appropriate choice. MATLAB automation server capabilities include the ability to execute commands in the MATLAB workspace, and to get and put matrices directly from and into the workspace.
4 MATLAB ActiveX Automation Server Support
4.1 MATLAB ActiveX Automation Methods
This section lists the methods that are supported by the MATLAB Automation Server. The data types for the arguments and return values are expressed as ActiveX Automation data types, which are language-independent types defined by the ActiveX Automation protocol. For example, BSTR is a wide-character string type defined as an Automation type, and is the same data format used by Visual Basic to store strings. Any ActiveX-compliant controller should support these data types, although the details of how you declare and manipulate these are controller specific.
BSTR Execute([in] BSTR Command);
This command accepts a single string (Command), which contains any command that can be typed at the MATLAB command window prompt. MATLAB will execute the command and return the results as a string.
void GetFullMatrix(
[in] BSTR Name,
[in] BSTR Workspace,
[in, out] SAFEARRAY(double)* pr,
[in, out] SAFEARRAY(double)* pi);
This method retrieves a full, one- or two-dimensional real or imaginary mxArray from the named workspace. The real and (optional) imaginary parts are retrieved into separate arrays of doubles.
Name. Identifies the name of the mxArray to be retrieved.
Workspace. Identifies the workspace that contains the mxArray. Use the workspace name "base" to retrieve an mxArray from the default MATLAB workspace. Use the workspace name "global" to put the mxArray into the global MATLAB workspace. The "caller" workspace does not have any context in the API when used outside of MEX-files.
pr. Array of reals that is dimensioned to be the same size as the mxArray being retrieved. On return, this array will contain the real values of the mxArray.
pi. Array of reals that is dimensioned to be the same size as the mxArray being retrieved. On return, this array will contain the imaginary values of the mxArray. If the requested mxArray is not complex, an empty array must be passed.
void PutFullMatrix(
[in] BSTR Name,
[in] BSTR Workspace,
[in] SAFEARRAY(double) pr,
[in] SAFEARRAY(double) pi);
This method puts a full, one- or two-dimensional real or imaginary mxArray into the named workspace. The real and (optional) imaginary parts are passed in through separate arrays of doubles.
Name. Identifies the name of the mxArray to be placed.
Workspace. Identifies the workspace into which the mxArray should be placed. Use the workspace name "base" to put the mxArray into the default MATLAB workspace. Use the workspace name "global" to put the mxArray into the global MATLAB workspace. The "caller" workspace does not have any context in the API when used outside of MEX-files.
pr. Array of reals that contains the real values for the mxArray.
pi. Array of reals that contains the imaginary values for the mxArray. If the mxArray that is being sent is not complex, an empty array must be passed for this parameter.
4.2 MATLAB ActiveX Automation Properties
You have the option of making your server application visible or not by setting the Visible property. When visible, the server window appears on the desktop, enabling the user to interact with the server application. This may be useful for such purposes as debugging. When not visible, the server window does not appear, thus perhaps making for a cleaner interface and also preventing any interaction with the server application.
By default, the Visible property is enabled, or set to 1. You can change the setting of Visible by setting it to 0 (invisible) or 1 (visible).
5 Using VC++ as Automation Client
To simply describe the usage of automation client in VC++, more source code are used as illustration in this section. The source code list here can be used as template to implement various tasks.
The Microsoft Foundation Classes (MFC) provide the COleDispatchDriver class along with VC++. The class COleDispatchDriver provides the principal support for the client side of Automation. In current case the client need dynamically (at run time) acquire information about the properties and operations of the server. And a user defined class has to be derived from ColeDispatchDriver manually. (Otherwise it can be generated by ClassWizard automatically if the type-library file describing the properties and functions of the server application’s object is available which is not provided by MATLAB.) The source code to define the derived class is listed below:
class CMatlab : public COleDispatchDriver
{
public:
CString Execute(LPCTSTR strCommand);
void GetFullMatrix(LPCTSTR Name, LPCTSTR Workspace,
VARTYPE vt, unsigned int cDims, SAFEARRAYBOUND FAR* rgsabound, void *pr, void *pi, unsigned sizeArray);
void PutFullMatrix(LPCTSTR Name, LPCTSTR Workspace,
VARTYPE vt, unsigned int cDims, SAFEARRAYBOUND FAR* rgsabound, void *pr, void *pi, unsigned sizeArray);
};
This class need to be initialised and attached to the MATLAB first. The name of the MATLAB ActiveX object that is placed in the registry is Matlab.Application. The code for initializing is listed below:
CMatlab m_oleMatlab
void InitialMatlabObject()
{
if (m_oleMatlab.m_lpDispatch == NULL)
{
COleException e;
CLSID clsid;
If (CLSIDFromProgID(OLESTR
("Matlab.Application"), &clsid) != NOERROR)
{
AfxMessageBox(IDP_CANNOT_CREATE_MATLAB);
}
LPUNKNOWN lpUnk;
LPDISPATCH lpDispatch;
if (GetActiveObject(clsid, NULL, &lpUnk) == NOERROR)
{
HRESULT hr = lpUnk-> QueryInterface(
IID_IDispatch, (LPVOID*)&lpDispatch);
lpUnk->Release();
if (hr == NOERROR)
m_oleMatlab.AttachDispatch(lpDispatch, TRUE);
}
if (!m_oleMatlab.CreateDispatch(
_T("Matlab.Application")))
{
AfxMessageBox(IDP_CANNOT_CREATE_MATLAB);
}
}
}
The last part is the implementation of class CMatlab. The details for using the three MATLAB methods in VC++ is described below:
CString CMatlab::Execute(LPCTSTR strCommand)
{
static BYTE parms[] =
VTS_BSTR;
CString result;
USES_CONVERSION;
OLECHAR FAR* szMember = T2OLE("Execute");
DISPID dispid;
m_lpDispatch->GetIDsOfNames(IID_NULL,
&szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
InvokeHelper(dispid, DISPATCH_METHOD, VT_BSTR,
(void *)&result, parms, strCommand);
return result;
}
void CMatlab::PutFullMatrix(LPCTSTR Name,
LPCTSTR Workspace, unsigned int cDims,
SAFEARRAYBOUND FAR* rgsabound, void *pr, void *pi,
unsigned sizeArray);
{
VARIANT Imaginary, Real;
Imaginary.vt = Real.vt = VT_ARRAY | VT_R8;
SAFEARRAY *pIArray, *pRArray;
pIArray = SafeArrayCreate(VT_R8, cDims, rgsabound);
pRArray = SafeArrayCreate(VT_R8, cDims, rgsabound);
SafeArrayLock(pIArray);
memcpy(pIArray->pvData, pi, sizeArray);
SafeArrayUnlock(pIArray);
SafeArrayLock(pRArray);
memcpy(pRArray->pvData, pr, sizeArray);
SafeArrayUnlock(pRArray);
Imaginary.parray = pIArray;
Real.parray = pRArray;
static BYTE parms[] =
VTS_BSTR VTS_BSTR VTS_VARIANT VTS_VARIANT;
USES_CONVERSION;
OLECHAR FAR* szMember = T2OLE("PutFullMatrix");
OLECHAR FAR* name = T2OLE(Name);
OLECHAR FAR* workspace = T2OLE(Workspace);
DISPID dispid;
m_lpDispatch->GetIDsOfNames(IID_NULL,
&szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
InvokeHelper(dispid, DISPATCH_METHOD, VT_EMPTY, NULL,
parms, Name, Workspace, &Real, &Imaginary);
}
void CMatlab::GetFullMatrix(LPCTSTR Name,
LPCTSTR Workspace, unsigned int cDims,
SAFEARRAYBOUND FAR* rgsabound, void *pr, void *pi,
unsigned sizeArray);
{
VARIANT Imaginary, Real;
Imaginary.vt = Real.vt = VT_ARRAY | VT_R8;
SAFEARRAY *pIArray, *pRArray;
pIArray = SafeArrayCreate(VT_R8, cDims, rgsabound);
pRArray = SafeArrayCreate(VT_R8, cDims, rgsabound);
Imaginary.parray = pIArray;
Real.parray = pRArray;
static BYTE parms[] =
VTS_BSTR VTS_BSTR VTS_VARIANT VTS_VARIANT;
USES_CONVERSION;
OLECHAR FAR* szMember = T2OLE("GetFullMatrix");
OLECHAR FAR* name = T2OLE(Name);
OLECHAR FAR* workspace = T2OLE(Workspace);
DISPID dispid;
m_lpDispatch->GetIDsOfNames(IID_NULL,
&szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
InvokeHelper(dispid, DISPATCH_METHOD, VT_EMPTY, NULL,
parms, Name, Workspace, &Real, &Imaginary);
SafeArrayLock(pIArray);
memcpy(pIArray->pvData, pi, sizeArray);
SafeArrayUnlock(pIArray);
SafeArrayLock(pRArray);
memcpy(pRArray->pvData, pr, sizeArray);
SafeArrayUnlock(pRArray);
}
The description of the parameters is:
Name: Pointer to a null-terminated string of bytes specifying the name of variable in MATLAB.
Workspace: Pointer to a null-terminated string of bytes specifying the workspace name
cDims: Number of dimensions in the array.
rgsabound: Pointer to a SAFEARRRAYBOUND structure.
The SAFEARRRAYBOUND structure is defined as following:
typedef struct tagSAFEARRAYBOUND {
unsigned long cElements;
long lLbound;
} SAFEARRAYBOUND;
It represents the bounds of one dimension of the array. The lower bound of the dimension is represented by lLbound, and cElements represents the number of elements in the dimension.
pr: Pointer to a one or two dimensional double array which contains the real part of data put to or got from MATLAB.
pi: Pointer to a one or two dimensional double array which contains the imaginary part of data put to or got from MATLAB.
One example to apply the PutFullMatrix function is shown below:
double matrix[8][256];
SAFEARRAYBOUND rgsabound[2];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 256;
rgsabound[1].lLbound = 0;
rgsabound[1].cElements = 8;
double pi[8][256] = {0};
m_oleMatlab.PutFullMatrix("A", "base", 2, rgsabound,
matrix, pi, (unsigned)sizeof(matrix));
One point should be noted that a two dimensional matrix with size in VC++ becomes m in MATLAB which is the transpose of the original one.
More information about using automation between MATLAB can be referenced in the External Interface/API section of the MATLAB document and Microsoft Development Network (MSDN).
2.Matlab的C/C++数学函数库,有一个mxArray类可以把C++的数据传递给Matlab.不过好像这个库调用不了工具箱的函数,它以一般的矩阵运算为主.
3.自动化接口,通过Matlab.Application或者自定义一个自动化组件.
VC下的OLEView工具可以查看到Matlab.Application组件的接口里面的函数主要
有
GetFullMatrix(...)
PutFullMatrix(...)
Excute(...).
数据的传递是通过自动化的数据类型SAFEARRAY进行.c的数据添加到SafeArray类型的数据就可以作为PutFullFMatrix的参数传递给Matlab处理.
VC下导入Matlab组件和ADO添加类型库预处理类似.
参考一下
www.eas.iis.fhg.de/publications/ slides/2002/015/slides.pdf
A Programming Issue: How to Interface MATLAB with Microsoft Visual C++
1 Introduction
MATLAB® is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.
Most of the research work in current project is implemented under Microsoft Visual C++ (VC++). VC++ is a powerful programming tool for Windows software development. Its programming is relatively low level and can interact with hardware and Operating System directly. Application interface can also be built efficiently under the development environment provided by VC++. Hence high performance software can be easily generated.
However, VC++ is a general programming tool and it does not provide abundant specialized routines as in MATLAB. Many specialized toolboxes coming with MATLAB such as Image Processing, Digital Signal Processing, and Neural Network, reduces much programming effort for researchers to do experiments in these fields. If an interface can be built between MATLAB and VC++, it would be more convenient for us to do the experiments and research work.
2 Possible routes to interface MATLAB with VC++
MATLAB provides interfaces to external routines written in other programming languages, data that needs to be shared with external routines, peripheral devices that communicate directly with MATLAB, and software applications ActiveX and Dynamic Data Exchange. Much of this interface capability was formerly referred to under the title of the MATLAB Application Program Interface, or API. These methods include:
Calling C and Fortran Programs from MATLAB - build C subroutines into callable MEX files.
Calling MATLAB from C Programs - use the MATLAB engine library to call MATLAB from C programs.
Importing and Exporting Data - importing data into and exporting data from the MATLAB environment using MAT-files.
ActiveX and DDE Support - use ActiveX and Dynamic Data Exchange (DDE) with MATLAB.
The ActiveX technique, also usually called OLE Automation in VC++, is the most flexible method among them. ActiveX is a set of object-oriented technologies and tools that allow software developers to integrate application-specific components from different vendors into their own application solution. ActiveX Control components can be both visually and programmatically integrated into an ActiveX control container, such as a MATLAB figure window. ActiveX Automation allows MATLAB to both control and be controlled by other ActiveX components. In the following sections the use of ActiveX to interface MATLAB with VC++ will be introduced in detail.
3 Introduction to ActiveX
ActiveX is a Microsoft Windows protocol for component integration. Using ActiveX, developers and end users can select application-specific, ActiveX components produced by different vendors and seamlessly integrate them into a complete application solution. For example, a single application may require database access, mathematical analysis, and presentation quality business graphs. Using ActiveX, a developer may choose a database access component by one vendor, a business graph component by another, and integrate these into a mathematical analysis package produced by a third.
ActiveX Concepts and Terminology COM
ActiveX is a family of related object-oriented technologies that have a common root, called the Component Object Model, or COM. Each object-oriented language or environment has an object model that defines certain characteristics of objects in that environment, such as how objects are located, instantiated, or identified. COM defines the object model for all ActiveX objects.
ActiveX Interfaces
Each ActiveX object supports one or more named interfaces. An interface is a logically related collection of methods, properties, and events. Methods are similar to function calls in that they are a request for the object to perform some action. Properties are state variables maintained by the object, such as the color of text, or the name of a file on which the control is acting. Events are notifications that the control forwards back to its client.
For example, the sample control shipped with MATLAB has the following methods, properties, and events:
· Methods
Redraw - causes the control to redraw
Beep - causes the control to beep
AboutBox - display the control's "About" dialog
· Properties
Radius (integer) - sets the radius of the circle drawn by the control
Label (string) - text to be drawn in the control
· Events
Click - fired when the user clicks on the control
One important characteristic of COM is that it defines an object model in which objects support multiple interfaces. Some interfaces are standard interfaces, which are defined by Microsoft and are part of ActiveX, and some interfaces are custom interfaces, which are defined by individual component vendors. In order to use any ActiveX object, you must learn about which custom interfaces it supports, and the interface's methods, properties, and events. The ActiveX object's vendor provides this information.
MATLAB supports two ActiveX technologies: ActiveX control containment and ActiveX Automation. ActiveX controls are application components that can be both visually and programmatically integrated into an ActiveX control container, such as MATLAB figure windows. Some examples of useful ActiveX controls are the Microsoft Internet Explorer Web Browser control, the Microsoft Windows Communications control for serial port access, and the graphical user interface controls delivered with the Visual Basic development environment.
ActiveX Automation allows MATLAB to both control and be controlled by other ActiveX components. When MATLAB is controlled by another component, it is acting as an automation server. When MATLAB controls another component, MATLAB is the automation client, and the other component is the automation server.
To utilize the capability of interface development in VC++ and the standard specialized functions in MATLAB the automation server is an appropriate choice. MATLAB automation server capabilities include the ability to execute commands in the MATLAB workspace, and to get and put matrices directly from and into the workspace.
4 MATLAB ActiveX Automation Server Support
4.1 MATLAB ActiveX Automation Methods
This section lists the methods that are supported by the MATLAB Automation Server. The data types for the arguments and return values are expressed as ActiveX Automation data types, which are language-independent types defined by the ActiveX Automation protocol. For example, BSTR is a wide-character string type defined as an Automation type, and is the same data format used by Visual Basic to store strings. Any ActiveX-compliant controller should support these data types, although the details of how you declare and manipulate these are controller specific.
BSTR Execute([in] BSTR Command);
This command accepts a single string (Command), which contains any command that can be typed at the MATLAB command window prompt. MATLAB will execute the command and return the results as a string.
void GetFullMatrix(
[in] BSTR Name,
[in] BSTR Workspace,
[in, out] SAFEARRAY(double)* pr,
[in, out] SAFEARRAY(double)* pi);
This method retrieves a full, one- or two-dimensional real or imaginary mxArray from the named workspace. The real and (optional) imaginary parts are retrieved into separate arrays of doubles.
Name. Identifies the name of the mxArray to be retrieved.
Workspace. Identifies the workspace that contains the mxArray. Use the workspace name "base" to retrieve an mxArray from the default MATLAB workspace. Use the workspace name "global" to put the mxArray into the global MATLAB workspace. The "caller" workspace does not have any context in the API when used outside of MEX-files.
pr. Array of reals that is dimensioned to be the same size as the mxArray being retrieved. On return, this array will contain the real values of the mxArray.
pi. Array of reals that is dimensioned to be the same size as the mxArray being retrieved. On return, this array will contain the imaginary values of the mxArray. If the requested mxArray is not complex, an empty array must be passed.
void PutFullMatrix(
[in] BSTR Name,
[in] BSTR Workspace,
[in] SAFEARRAY(double) pr,
[in] SAFEARRAY(double) pi);
This method puts a full, one- or two-dimensional real or imaginary mxArray into the named workspace. The real and (optional) imaginary parts are passed in through separate arrays of doubles.
Name. Identifies the name of the mxArray to be placed.
Workspace. Identifies the workspace into which the mxArray should be placed. Use the workspace name "base" to put the mxArray into the default MATLAB workspace. Use the workspace name "global" to put the mxArray into the global MATLAB workspace. The "caller" workspace does not have any context in the API when used outside of MEX-files.
pr. Array of reals that contains the real values for the mxArray.
pi. Array of reals that contains the imaginary values for the mxArray. If the mxArray that is being sent is not complex, an empty array must be passed for this parameter.
4.2 MATLAB ActiveX Automation Properties
You have the option of making your server application visible or not by setting the Visible property. When visible, the server window appears on the desktop, enabling the user to interact with the server application. This may be useful for such purposes as debugging. When not visible, the server window does not appear, thus perhaps making for a cleaner interface and also preventing any interaction with the server application.
By default, the Visible property is enabled, or set to 1. You can change the setting of Visible by setting it to 0 (invisible) or 1 (visible).
5 Using VC++ as Automation Client
To simply describe the usage of automation client in VC++, more source code are used as illustration in this section. The source code list here can be used as template to implement various tasks.
The Microsoft Foundation Classes (MFC) provide the COleDispatchDriver class along with VC++. The class COleDispatchDriver provides the principal support for the client side of Automation. In current case the client need dynamically (at run time) acquire information about the properties and operations of the server. And a user defined class has to be derived from ColeDispatchDriver manually. (Otherwise it can be generated by ClassWizard automatically if the type-library file describing the properties and functions of the server application’s object is available which is not provided by MATLAB.) The source code to define the derived class is listed below:
class CMatlab : public COleDispatchDriver
{
public:
CString Execute(LPCTSTR strCommand);
void GetFullMatrix(LPCTSTR Name, LPCTSTR Workspace,
VARTYPE vt, unsigned int cDims, SAFEARRAYBOUND FAR* rgsabound, void *pr, void *pi, unsigned sizeArray);
void PutFullMatrix(LPCTSTR Name, LPCTSTR Workspace,
VARTYPE vt, unsigned int cDims, SAFEARRAYBOUND FAR* rgsabound, void *pr, void *pi, unsigned sizeArray);
};
This class need to be initialised and attached to the MATLAB first. The name of the MATLAB ActiveX object that is placed in the registry is Matlab.Application. The code for initializing is listed below:
CMatlab m_oleMatlab
void InitialMatlabObject()
{
if (m_oleMatlab.m_lpDispatch == NULL)
{
COleException e;
CLSID clsid;
If (CLSIDFromProgID(OLESTR
("Matlab.Application"), &clsid) != NOERROR)
{
AfxMessageBox(IDP_CANNOT_CREATE_MATLAB);
}
LPUNKNOWN lpUnk;
LPDISPATCH lpDispatch;
if (GetActiveObject(clsid, NULL, &lpUnk) == NOERROR)
{
HRESULT hr = lpUnk-> QueryInterface(
IID_IDispatch, (LPVOID*)&lpDispatch);
lpUnk->Release();
if (hr == NOERROR)
m_oleMatlab.AttachDispatch(lpDispatch, TRUE);
}
if (!m_oleMatlab.CreateDispatch(
_T("Matlab.Application")))
{
AfxMessageBox(IDP_CANNOT_CREATE_MATLAB);
}
}
}
The last part is the implementation of class CMatlab. The details for using the three MATLAB methods in VC++ is described below:
CString CMatlab::Execute(LPCTSTR strCommand)
{
static BYTE parms[] =
VTS_BSTR;
CString result;
USES_CONVERSION;
OLECHAR FAR* szMember = T2OLE("Execute");
DISPID dispid;
m_lpDispatch->GetIDsOfNames(IID_NULL,
&szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
InvokeHelper(dispid, DISPATCH_METHOD, VT_BSTR,
(void *)&result, parms, strCommand);
return result;
}
void CMatlab::PutFullMatrix(LPCTSTR Name,
LPCTSTR Workspace, unsigned int cDims,
SAFEARRAYBOUND FAR* rgsabound, void *pr, void *pi,
unsigned sizeArray);
{
VARIANT Imaginary, Real;
Imaginary.vt = Real.vt = VT_ARRAY | VT_R8;
SAFEARRAY *pIArray, *pRArray;
pIArray = SafeArrayCreate(VT_R8, cDims, rgsabound);
pRArray = SafeArrayCreate(VT_R8, cDims, rgsabound);
SafeArrayLock(pIArray);
memcpy(pIArray->pvData, pi, sizeArray);
SafeArrayUnlock(pIArray);
SafeArrayLock(pRArray);
memcpy(pRArray->pvData, pr, sizeArray);
SafeArrayUnlock(pRArray);
Imaginary.parray = pIArray;
Real.parray = pRArray;
static BYTE parms[] =
VTS_BSTR VTS_BSTR VTS_VARIANT VTS_VARIANT;
USES_CONVERSION;
OLECHAR FAR* szMember = T2OLE("PutFullMatrix");
OLECHAR FAR* name = T2OLE(Name);
OLECHAR FAR* workspace = T2OLE(Workspace);
DISPID dispid;
m_lpDispatch->GetIDsOfNames(IID_NULL,
&szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
InvokeHelper(dispid, DISPATCH_METHOD, VT_EMPTY, NULL,
parms, Name, Workspace, &Real, &Imaginary);
}
void CMatlab::GetFullMatrix(LPCTSTR Name,
LPCTSTR Workspace, unsigned int cDims,
SAFEARRAYBOUND FAR* rgsabound, void *pr, void *pi,
unsigned sizeArray);
{
VARIANT Imaginary, Real;
Imaginary.vt = Real.vt = VT_ARRAY | VT_R8;
SAFEARRAY *pIArray, *pRArray;
pIArray = SafeArrayCreate(VT_R8, cDims, rgsabound);
pRArray = SafeArrayCreate(VT_R8, cDims, rgsabound);
Imaginary.parray = pIArray;
Real.parray = pRArray;
static BYTE parms[] =
VTS_BSTR VTS_BSTR VTS_VARIANT VTS_VARIANT;
USES_CONVERSION;
OLECHAR FAR* szMember = T2OLE("GetFullMatrix");
OLECHAR FAR* name = T2OLE(Name);
OLECHAR FAR* workspace = T2OLE(Workspace);
DISPID dispid;
m_lpDispatch->GetIDsOfNames(IID_NULL,
&szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
InvokeHelper(dispid, DISPATCH_METHOD, VT_EMPTY, NULL,
parms, Name, Workspace, &Real, &Imaginary);
SafeArrayLock(pIArray);
memcpy(pIArray->pvData, pi, sizeArray);
SafeArrayUnlock(pIArray);
SafeArrayLock(pRArray);
memcpy(pRArray->pvData, pr, sizeArray);
SafeArrayUnlock(pRArray);
}
The description of the parameters is:
Name: Pointer to a null-terminated string of bytes specifying the name of variable in MATLAB.
Workspace: Pointer to a null-terminated string of bytes specifying the workspace name
cDims: Number of dimensions in the array.
rgsabound: Pointer to a SAFEARRRAYBOUND structure.
The SAFEARRRAYBOUND structure is defined as following:
typedef struct tagSAFEARRAYBOUND {
unsigned long cElements;
long lLbound;
} SAFEARRAYBOUND;
It represents the bounds of one dimension of the array. The lower bound of the dimension is represented by lLbound, and cElements represents the number of elements in the dimension.
pr: Pointer to a one or two dimensional double array which contains the real part of data put to or got from MATLAB.
pi: Pointer to a one or two dimensional double array which contains the imaginary part of data put to or got from MATLAB.
One example to apply the PutFullMatrix function is shown below:
double matrix[8][256];
SAFEARRAYBOUND rgsabound[2];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 256;
rgsabound[1].lLbound = 0;
rgsabound[1].cElements = 8;
double pi[8][256] = {0};
m_oleMatlab.PutFullMatrix("A", "base", 2, rgsabound,
matrix, pi, (unsigned)sizeof(matrix));
One point should be noted that a two dimensional matrix with size in VC++ becomes m in MATLAB which is the transpose of the original one.
More information about using automation between MATLAB can be referenced in the External Interface/API section of the MATLAB document and Microsoft Development Network (MSDN).
收藏:
QQ书签
del.icio.us
最新评论
-
2005-05-08 08:20:48
不错的东东!
-
2005-05-08 08:22:39 http://ydcom.ycool.com/
我的MSN是
ydcom@hotmail.com
希望加我! -
2005-05-08 15:24:20
我这边msn不是很方便
如果有兴趣可以加我的qq
77374758
