Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data from matlab files into C

Tags:

c

matlab

I'm trying to learn how to use the C API to reading Matlab .mat files, but it's not working as I expected:

I'd like to just open a very simple .mat file called test.mat, read a value from the file and store it in a C variable. I've created test.mat in Matlab using the following commands:

> value = 3;
> save ("test.mat", "value")

Below is my C code, which doesn't even compile - the compiler doesn't seem to find the header files. See below the code for compiler output. What am I doing wrong here?

Code:

#include <stdlib.h>
#include <stdio.h>
#include <mat.h>
#include <matrix.h>

int main(int argc, char *argv[]) {
    double value;
    MATFile *datafile;
    datafile = matOpen("test.mat", "r");

    mxArray *mxValue;
    mxValue = matGetVariable(datafile, "value");

    matClose(datafile);
    value = *mxGetPr(mxArray);

    mxFree(mxArray);

    printf("The value fetched from the .mat file was: %f", value);

    return 0;
}

Compiler output:

$ make animate_shot
cc  -I/usr/local/MATLAB/R2011a/extern/include/   animate_shot.c   -o animate_shot
/tmp/cczrh1vT.o: In function `main':
animate_shot.c:(.text+0x1a): undefined reference to `matOpen'
animate_shot.c:(.text+0x2f): undefined reference to `matGetVariable'
animate_shot.c:(.text+0x3f): undefined reference to `matClose'
animate_shot.c:(.text+0x4b): undefined reference to `mxGetPr'
animate_shot.c:(.text+0x5e): undefined reference to `mxFree'
collect2: ld returned 1 exit status
make: *** [animate_shot] Error 1

(the -I flag is specified with the line CPPFLAGS=-I/usr/local/MATLAB/R2011a/extern/include/ in my makefile, and I've verified that the directory exists and contains the header files mat.h and matrix.h).

UPDATE:
I've found that the libraries I need to link in are libmat.so and libmx.so (according to this MathWorks help article), residing in /usr/local/MATLAB/R2011a/bin/glnxa64/ on my system. I've therefore updated my makefile to this:

CPPFLAGS =-I/usr/local/MATLAB/R2011a/extern/include/
LDFLAGS = -L/usr/local/MATLAB/R2011a/bin/glnxa64 -l mat -l mx

Now, running make gives the following command:

cc  -I/usr/local/MATLAB/R2011a/extern/include/ -L/usr/local/MATLAB/R2011a/bin/glnxa64 -l mat -l mx  animate_shot.c   -o animate_shot

However, I still get the same errors. Any ideas?

like image 560
Tomas Aschan Avatar asked Aug 30 '12 16:08

Tomas Aschan


1 Answers

This is a linker failure, not a compiler failure (and is unrelated to -I compiler option). You need to specify the directory in which the matlab .so files are located using -L flag and add a -l<matlab-lib-name> option to end of the compiler command that specifies the name of the matlab library.

For example:

cc -I/usr/local/MATLAB/R2011a/extern/include/ -L/usr/local/MATLAB/R2011a/lib animate_shot.c -o animate_shot -lmatlab

(I don't know the exact directory into the which .so are located or the name of the matlab library)


Based on the comment providing further information:

cc -I/usr/local/MATLAB/R2011a/extern/include/ -L/usr/local/MATLAB/R2011a/bin/glnxa64 animate_shot.c -o animate_shot -lmat -lmx

like image 155
hmjd Avatar answered Oct 09 '22 22:10

hmjd