MEXPLUS
=======
[![Build Status](https://travis-ci.org/kyamagu/mexplus.svg?branch=master)](https://travis-ci.org/kyamagu/mexplus)
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
C++ Matlab MEX development kit.
The kit contains a couple of C++ classes and macros to make MEX development
easy in Matlab. There are 3 major components in the development kit.
* `mexplus/dispatch.h` Macros to dispatch function calls within a MEX binary.
* `mexplus/arguments.h` MEX function argument wrappers.
* `mexplus/mxarray.h` MxArray data conversion and access class.
All classes are located in `mexplus` namespace, and you can use all of them by
including the `mexplus.h` header file.
The library depends on a few C++11 features, and might not be compatible with
older compilers. For older `g++`, make sure to add `-std=c++11` flag at compile
time, or in the `CXXFLAGS` variable in the MEX options located at
`$HOME/.matlab/$VERSION/mexopts.sh`, or in Matlab R2014a or later, at
`$HOME/.matlab/$VERSION/mex_C++_$ARCH.xml`.
Example
-------
Suppose we have the following Database class in C++, and we would like to create a Matlab wrapper.
```c++
// Database.h
// Hypothetical database class to be wrapped.
class Database {
public:
Database(const std::string& filename);
virtual ~Database();
std::string query(const std::string& key) const;
};
```
We will need to create two files.
* `Database_.cc`: C++ interface file.
* `Database.m`: Matlab interface file.
`Database_.cc`
C++ implementation of the MEX interface. It provides MEX entry points by
`MEX_DEFINE` macros and `MEX_DISPATCH` macro at the end. Notice how inputs and
outputs are wrapped by mexplus `InputArguments` and `OutputArguments` class.
They automatically convert majority of C++ types to/from `mxArray`, using C++
template. The `Session` class keeps `Database` instances between MEX calls,
allowing the MEX binary to be stateful.
```c++
// Database_.cc: C++ interface file to the Database class.
#include <mexplus.h>
#include "Database.h"
using namespace mexplus;
using namespace std;
// This initializes a session storage for Database instances.
template class mexplus::Session<Database>;
// Create a new instance of Database and return its session id.
MEX_DEFINE(new) (int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[]) {
InputArguments input(nrhs, prhs, 1);
OutputArguments output(nlhs, plhs, 1);
output.set(0, Session<Database>::create(
new Database(input.get<string>(0))));
}
// Delete the Database instance specified by its id.
MEX_DEFINE(delete) (int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[]) {
InputArguments input(nrhs, prhs, 1);
OutputArguments output(nlhs, plhs, 0);
Session<Database>::destroy(input.get(0));
}
// Query to the Database instance specified by its id with a string argument.
MEX_DEFINE(query) (int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[]) {
InputArguments input(nrhs, prhs, 2);
OutputArguments output(nlhs, plhs, 1);
const Database& database = Session<Database>::getConst(input.get(0));
output.set(0, database.query(input.get<string>(1)));
}
MEX_DISPATCH
```
`Database.m`
Matlab class interface file. The `id_` property keeps the session ID (handle)
in the MEX binary. Each method is a wrapper around corresponding MEX entry
points defined in the C++ file. The first argument of `Database_()` MEX function
is the name defined using `MEX_DEFINE()` macro in the above file.
```matlab
classdef Database < handle
%DATABASE Matlab interface to Database.
properties (Access = private)
id_ % ID of the session instance.
end
methods
function this = Database(filename)
%DATABASE Create a new database.
assert(ischar(filename));
this.id_ = Database_('new', filename);
end
function delete(this)
%DELETE Destructor.
Database_('delete', this.id_);
end
function result = query(this, key)
%QUERY Query something to the database.
assert(isscalar(this));
result = Database_('query', this.id_, key);
end
end
end
```
_Build_
The above C++ can be compiled by `mex` command. The output name `Database_` is
the MEX function name used in `Database.m`.
```matlab
mex -Iinclude Database_.cc -output Database_
```
In Linux, you might need to add `CXXFLAGS="$CXXFLAGS -std=c++11"` to `mex`
command. i.e.,
```bash
mex -Iinclude Database_.cc -output Database_ CXXFLAGS="\$CXXFLAGS -std=c++11"
```
Once compiled, the Database class is available in Matlab.
```matlab
database = Database('mydatabase.db');
result = database.query('something');
clear database;
```
The development kit also contains `make.m` build function to make a build
process easier. Customize this file to build your own MEX interface. The kit
depends on some of the C++11 features.
See `example` directory for a complete demonstration.
Dispatching calls
-----------------
MEXPLUS defines a few macros in `mexplus/dispatch.h` that help to create a
single MEX binary with multiple function entries. Create a C++ file that looks
like this:
```c++
//mylibrary.cc
#include <mexplus/dispatch.h>
MEX_DEFINE(myfunc) (int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[]) {
// Do something.
}
MEX_DEFINE(myfunc2) (int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[]) {
// Do another thing.
}
MEX_DISPATCH
```
Notice how `MEX_DEFINE` and `MEX_DISPATCH` macros are used. Then build this
file in Matlab.
```matlab
mex -Iinclude mylibrary.cc
```
The built MEX binary can now call two entries by the first argument.
Note that `MEX_DISPATCH` is only required per MEX binary. If you split the
`MEX_DEFINE` entries across multiple files, you only need to instantiate
`MEX_DISPATCH` in one file.
```matlab
mylibrary('myfunc', varargin{:}) % myfunc is called.
mylibrary('myfunc2', varargin{:}) % myfunc2 is called.
```
To prevent from unexpected use, it is a good practice to wrap these MEX calls
in a Matlab class or namescoped functions and place the MEX binary in a private
directory:
@MyClass/MyClass.m
@MyClass/myfunc.m
@MyClass/myfunc2.m
@MyClass/private/mylibrary.mex*
or,
+mylibrary/myfunc.m
+mylibrary/myfunc2.m
+mylibrary/private/mylibrary.mex*
Inside of `myfunc.m` and `myfunc2.m`, call the `mylibrary` MEX binary. This
design pattern is useful to wrap a C++ class in Matlab. See the `example`
directory in the package.
Parsing function arguments
--------------------------
MEXPLUS provides `InputArguments` and `OutputArguments` classes to ease
parsing, validation, and data conversion of input and output arguments to MEX
functions.
### InputArguments
The class provides a wrapper around input arguments to validate and convert
Matlab variables. You can define a single or multiple input formats to accept.
The `get()` method automatically converts Matlab's `mxArray` to most of the
basic C++ types using a template parameter. Also it can convert to a custom
data type when a template specialization to `MxArray::to()` method is provided.
(See the next section.)
__Example__: The MEX function takes a single numeric input argument.
```c++
// C++
InputArguments input(nrhs, prhs, 1);
myFunction(input.get<double>(0));
```
```matlab
% Matlab
myFunction(1.0);
```
__Example__: The MEX function takes two numeric arguments, and two optional
arguments specified by name-value pairs. When optional arguments are not given,
the function uses a default value.
```c++
// C++
InputArguments input(nrhs, prhs, 2, 2, "option1", "option2");
myFunction(input.get<double>(0),
input.get<int>(1),
input.get<string>("option1", "foo"), // default: "foo".
input.get<int>("option2", 10)); // default: 10.
```
```matlab
% Matlab
myFunction(1.0, 2);
myFunction(1.0, 2, 'option2', 11);
myFunction(1.0, 2, 'option1', 'bar');
myFunction(1.0, 2, 'option1', 'baz', 'option2', 12);
myFuncti
没有合适的资源?快使用搜索试试~ 我知道了~
毕业设计&课设-C++Matlab MEX开发工具包。.zip
共20个文件
cc:8个
h:5个
m:4个
需积分: 1 1 下载量 112 浏览量
2024-01-08
22:28:03
上传
评论
收藏 33KB ZIP 举报
温馨提示
matlab算法,工具源码,适合毕业设计、课程设计作业,所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答! matlab算法,工具源码,适合毕业设计、课程设计作业,所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答! matlab算法,工具源码,适合毕业设计、课程设计作业,所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答! matlab算法,工具源码,适合毕业设计、课程设计作业,所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答! matlab算法,工具源码,适合毕业设计、课程设计作业,所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答! matlab算法,工具源码,适合毕业设计、课程设计作业,所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计&课设-C++Matlab MEX开发工具包。.zip (20个子文件)
matlab_code
include
mexplus.h 234B
mexplus
arguments.h 14KB
mxtypes.h 11KB
dispatch.h 11KB
mxarray.h 57KB
.travis.yml 355B
make.m 4KB
test
testAll.m 2KB
testSession.cc 2KB
testString.cc 365B
testMxArray.cc 10KB
testDispatch.cc 597B
testArguments.cc 5KB
testMxTypes.cc 6KB
.gitignore 41B
example
Database.m 1KB
private
Database_.cc 2KB
Environment_.cc 2KB
runDatabase.m 463B
README.md 13KB
共 20 条
- 1
资源评论
白话机器学习
- 粉丝: 9739
- 资源: 7681
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功