Have you ever wanted to call subroutines from a C library from a scripting language like Matlab or octave or tela? Have you ever wanted to write your own simulation in C++ and have its functions be directly callable from Matlab, instead of going through output files? It is very tedious and error-prone to write the wrapper functions which convert from Matlab's types to the float or double or int types that your functions actually want.
Matwrap is a perl script that generates wrapper functions for you automatically from your .h files. Suppose, for example, that you've defined a C++ class like this:
class Simulator {
public:
Simulator(int a, float b);
int do_something();
~Simulator();
double param;
private:
// ...
};
matwrap defines the Matlab/octave/whatever functions
sim_ptr = Simulator_new(a, b) result = Simulator_do_something(sim_ptr) Simulator_delete(sim_ptr) param_value = Simulator_get_param(sim_ptr) Simulator_set_param(sim_ptr, param_value)which call the member functions or set the data member. All you have to do is to provide the C++ definition of your class to the program, exactly as shown above. You don't have to learn anything about Matlab or octave internals.
matwrap also automatically vectorizes your functions. Suppose, for example, you have a function like this:
double my_special_func(double arg1, double arg2, double arg3);You can supply a vector or matrix argument for either arg1 or arg2, like this in Matlab:
>> A = [1, 2; 3, 4]; B = [5, 6; 7, 8]; C = 9; >> res = my_special_func(A, B, C);The C function my_special_func is called 4 times, once for each element of A and B, and the result is returned as a matrix. This happens automatically, without any special declarations in your code at all.
You can also supply a vector argument, like this:
void plot_vector(int n_elements, float *vec); //%input vec(n_elements)In this case, by adding the comment "//%input...", you tell matwrap that the calling sequence from Matlab should be
plot_vector(xyz)and that
n_elements should be computed from the number of
rows of the vector xyz.
matwrap currently supports the languages Matlab (version 5), octave, and tela. Other languages can be added with a little bit of work.
The idea for matwrap came from Dave Beazley's SWIG, a wrapper generator for non-matrix languages like perl, python, and TCL. Wrapper generation for matrix languages requires a number of additional features not present in SWIG, so this wrapper generator was written. Hopefully the features found in matwrap will eventually be incorporated into SWIG, but for the moment it is entirely separate.