├── .gitignore
├── LICENSE
├── MinGW
├── README.md
├── mex_C++_mingw-w64.xml
├── mingw-w64_vars.bat
└── mingw_mexopts.bat
├── README.md
├── cppClass
├── class_wrapper_template.cpp
├── cppclass.m
├── pqheap.cpp
├── pqheap.hpp
└── pqheap.m
└── propertySheets
├── MATLAB.props
├── MATLABx64.props
├── MATLABx86.props
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .vscode/
2 | *.mexw64
3 | *.mexw32
4 | *.mexmaci64
5 | *.mexa64
6 | *.lib
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2016 Jonathan Chappelow
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/MinGW/README.md:
--------------------------------------------------------------------------------
1 | These files are used to help setup a MinGW distribution such as MinGW-w64 or TDM-GCC to compile MATLAB MEX files. For detailed instructions, see the following Stack Overflow answer:
2 |
3 | [Using GCC (MinGW) as MATLAB's MEX compiler](http://stackoverflow.com/a/28490382/2778484)
4 |
--------------------------------------------------------------------------------
/MinGW/mex_C++_mingw-w64.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
24 |
43 |
48 |
49 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
101 |
107 |
108 |
--------------------------------------------------------------------------------
/MinGW/mingw-w64_vars.bat:
--------------------------------------------------------------------------------
1 | rem @echo off
2 | set MW_TARGET_ARCH=win64
3 | set MINGWROOT=C:\mingw-w64\x86_64-4.9.2-release-posix-seh-rt_v3-rev1
4 | set PATH=%MINGWROOT%\bin;%PATH%
5 |
--------------------------------------------------------------------------------
/MinGW/mingw_mexopts.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 |
3 | REM MinGW-w64 C++ mexopts.bat
4 | REM by Jonathan Chappelow (chappjc)
5 | REM re: http://stackoverflow.com/a/28490382/2778484
6 | REM This is the legacy MEX configuration method. See the XML version at:
7 | REM https://github.com/chappjc/MATLAB/blob/master/MinGW/mex_C%2B%2B_mingw-w64.xml
8 |
9 | REM Edit MINGWROOT or set an environment variable
10 | set MINGWROOT=C:\ProgFiles\MinGW-w64\mingw64
11 | set PATH=%MINGWROOT%\bin;%PATH%
12 |
13 | set MATLAB=%MATLAB_ROOT%
14 | set MW_TARGET_ARCH=win64
15 |
16 | set COMPILER=x86_64-w64-mingw32-g++
17 | set COMPFLAGS=-c -m64 -mwin32 -mdll -Wall -DMATLAB_MEX_FILE
18 | REM Optionally add -std=c++11 to COMPFLAGS (-std=c99 for C)
19 | set OPTIMFLAGS=-DNDEBUG -O3
20 | set DEBUGFLAGS=-g
21 | set NAME_OBJECT=-o
22 |
23 | set LIBLOC=%MATLAB%\extern\lib\%MW_TARGET_ARCH%\microsoft
24 | set LINKER=x86_64-w64-mingw32-g++
25 | set LINKFLAGS=-shared -L"%LIBLOC%" -L"%MATLAB%\bin\win64"
26 | REM Optionally add to LINKFLAGS after -shared to get standalone output: -static-libstdc++ -static-libgcc
27 | set LINKFLAGSPOST=-lmx -lmex -leng -lmat -lmwlapack -lmwblas
28 | set LINKOPTIMFLAGS=-O3
29 | set LINKDEBUGFLAGS=-g
30 | set LINK_FILE=
31 | set LINK_LIB=
32 | set NAME_OUTPUT=-o "%OUTDIR%%MEX_NAME%%MEX_EXT%"
33 |
34 | set RC_COMPILER=
35 | set RC_LINKER=
36 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | MATLAB
2 | ======
3 |
4 | The repository contains a few small projects designed to help utilize C++ code in MATLAB. Currently there are some compiler configurations and a class wrapper:
5 | 1. **MinGW** contains configuration files to allow the use of MinGW's GCC compiler's to build MEX files.
6 | 2. **cppClass** contains an example of how to wrap a C++ class by MATLAB MEX file, with a bonus MATLAB class to manage the interface and lifetime of the underlying instances.
7 | 3. **propertySheets** contains Visual Studio property sheets for setting up projects to build MEX files from Visual Studio directly.
8 |
9 | See also [my GitHub Gists](https://gist.github.com/chappjc).
10 |
--------------------------------------------------------------------------------
/cppClass/class_wrapper_template.cpp:
--------------------------------------------------------------------------------
1 | // WORK IN PROGRESS!
2 | //
3 | // class_wrapper_template.cpp
4 | // Example of using a C++ class via a MEX-file
5 | // by Jonathan Chappelow (chappjc)
6 | //
7 | // Design goals:
8 | // 1. Manage multiple persistent instances of a C++ class
9 | // 2. Small consecutive integer handles used in MATLAB (not cast pointers)
10 | // 3. Transparently handle resource management (i.e. MATLAB never
11 | // responsible for memory allocated for C++ classes)
12 | // a. No memory leaked if MATLAB fails to issue "delete" action
13 | // b. Automatic deallocation if MEX-file prematurely unloaded
14 | // 4. Guard against premature module unloading
15 | // 5. Validity of handles implicitly verified without checking a magic number
16 | // 6. No wrapper class or functions mimicking mexFunction, just an intuitive
17 | // switch-case block in mexFunction.
18 | //
19 | // Note that these goals should be acheved without regard to any MATLAB class,
20 | // but which can also help address memory management issues. As such, the
21 | // resulting MEX-file can safely be used directly (but not too elegantly).
22 | //
23 | // Use:
24 | // 1. Enumerate the different actions (e.g. New, Delete, Insert, etc.) in the
25 | // Actions enum. For each enumerated action, specify a string (e.g.
26 | // "new", "delete", "insert", etc.) to be passed as the first argument to
27 | // the MEX function in MATLAB.
28 | // 2. Customize the handling for each action in the switch statement in the
29 | // body of mexFunction (e.g. call the relevant C++ class method).
30 | //
31 | // Implementation:
32 | //
33 | // For your C++ class, class_type, mexFunction uses static data storage to hold
34 | // a persistent (between calls to mexFunction) table of integer handles and
35 | // smart pointers to dynamically allocated class instances. A std::map is used
36 | // for this purpose, which facilitates locating known handles, for which only
37 | // valid instances of your class are guaranteed to exist:
38 | //
39 | // typedef unsigned int handle_type;
40 | // std::map>
41 | //
42 | // A std::shared_ptr takes care of deallocation when either (1) a table element
43 | // is erased via the "delete" action or (2) the MEX-file is unloaded.
44 | //
45 | // To prevent the MEX-file from unloading while a MATLAB class instances exist,
46 | // mexLock is called each time a new C++ class instance is created, adding to
47 | // the MEX-file's lock count. Each time a C++ instance is deleted mexUnlock is
48 | // called, removing one lock from the lock count.
49 | //
50 | // Requirements:
51 | //
52 | // A modern compiler with the following C++11 features:
53 | // - shared_ptr
54 | // - auto
55 | // - enum class
56 | // - initializer_list (for const map initialization)
57 | // (VS2013, recent GCC possibly with -std=c++11, Clang since 3.1)
58 | //
59 | // TODO:
60 | //
61 | // - This example uses a priority queue class of mine, which is far from the
62 | // simplest example. Demonstrate with something more basic.
63 | // - Somehow put the relevant parts in a header, OR the other way around -- put
64 | // user class config in a header and include into the mexFunction's cpp.
65 | //
66 | // 04/25/15 (chappjc) - Initial version.
67 |
68 | #include "mex.h"
69 |
70 | #include
71 | #include