Christian's Projects » MATLAB http://www.incasoftware.de/~kamm/projects Mon, 26 Jun 2017 20:29:06 +0000 en-US hourly 1 http://wordpress.org/?v=3.5.1 MATLAB file access http://www.incasoftware.de/~kamm/projects/index.static/2007/10/05/matlab-file-access/ http://www.incasoftware.de/~kamm/projects/index.static/2007/10/05/matlab-file-access/#comments Fri, 05 Oct 2007 19:51:57 +0000 Christian http://www.incasoftware.de/~kamm/projects/index.static/2007/10/05/matlab-file-access/ Quite a while ago I was looking at ways to interface between MATLAB and a C++ program for a job at university. In the end I wrote a C++ wrapper for the C API to access MAT files which turned out to be quite useful. Since it’s small and easy to use, I decided to release it.

At first the plan was to make the program into a MEX file (basically a dynamically linked extension program), but I quickly ran into a problem with deal II (an excellent, well documented and object-oriented library for solving partial differential equations with adaptive finite elements): MATLAB altered some of the floating point precision settings which lead to an algorithm in deal II running in an infinite loop (the epsilon value was hardcoded at that time and basically became zero). While the bug was promptly fixed, there were other issues and it was decided that embedding the program was not worth the trouble.

Long story short, I needed a way to read and write the MAT files that MATLAB generates and loads with the ‘load’ and ‘save’ commands. Using the C API directly seemed cumbersome and error prone, so this C++ wrapper was born.

Download it (4 kB) as .tar.bz2 or as zip.

A basic use case looks like this (also check out test.cc):

#include "matlabfile.h"
using namespace MatlabUtilities;
 
File matfile("some.mat");
std::vector<double> vec1;
matfile.read<Types::real_vector>("v1", vec1);
 
double[100] vec2;
matfile.read<Types::fixed_length_real_vector<100> >("v2", vec2);
 
matfile.write<Types::string>("hw", "Hello World!");

Not all MATLAB types are implemented (check Types namespace in matlabaccessors.h for a list), in particular real matrices with a column and row length different from one are missing for two reasons: I didn’t need them for the job and you probably have your own preferred matrix type anyway. But don’t worry, the template-design makes it easy to add these missing features. What needs to be implemented is

struct real_matrix {};
 
template <>
struct Reader<real_matrix, MyMatrix> {
    static void read(mxArray* var, MyMatrix& result);
}
 
template <>
struct Writer<real_matrix, MyMatrix> {
    static mxArray* write(MyMatrix& source);
}
 
template <>
struct TypeCheck<real_matrix> {
    static bool typeCheck(mxArray* var);
}

where the ‘read’ function reads data from the mxArray into result, ‘write’ returns a new mxArray filled with source’s data and ‘typeCheck’ returns true if the mxArray contains a real_matrix. With these template specializations in place you can use the new code like this:

MyMatrix mat;
matfile.read<real_matrix>("somevarname", mat);
matfile.write<real_matrix>("someother", mat);

Similarly, you can add a new reader to say ‘real_vector’ to accomodate for your own vector type.

The code snippet only contains code for reading and writing data. Other management functionality, like listing all fields in a MAT file is not implemented. I hope it will nevertheless be useful for someone.

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2007/10/05/matlab-file-access/feed/ 5