Note that there are some explanatory texts on larger screens.

plurals
  1. POReading data from matlab files into C
    primarykey
    data
    text
    <p>I'm trying to learn how to use the C API to reading Matlab <code>.mat</code> files, but it's not working as I expected:</p> <p>I'd like to just open a very simple <code>.mat</code> file called <code>test.mat</code>, read a value from the file and store it in a C variable. I've created <code>test.mat</code> in Matlab using the following commands:</p> <pre><code>&gt; value = 3; &gt; save ("test.mat", "value") </code></pre> <p>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?</p> <p><strong>Code:</strong></p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;mat.h&gt; #include &lt;matrix.h&gt; 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; } </code></pre> <p><strong>Compiler output:</strong></p> <pre><code>$ 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 </code></pre> <p>(the -I flag is specified with the line <code>CPPFLAGS=-I/usr/local/MATLAB/R2011a/extern/include/</code> in my makefile, and I've verified that the directory exists and contains the header files <code>mat.h</code> and <code>matrix.h</code>).</p> <p><strong>UPDATE:</strong><br> I've found that the libraries I need to link in are <code>libmat.so</code> and <code>libmx.so</code> (according to <a href="http://www.mathworks.se/help/techdoc/matlab_external/f19027.html">this MathWorks help article</a>), residing in <code>/usr/local/MATLAB/R2011a/bin/glnxa64/</code> on my system. I've therefore updated my makefile to this:</p> <pre><code>CPPFLAGS =-I/usr/local/MATLAB/R2011a/extern/include/ LDFLAGS = -L/usr/local/MATLAB/R2011a/bin/glnxa64 -l mat -l mx </code></pre> <p>Now, running <code>make</code> gives the following command:</p> <pre><code>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 </code></pre> <p>However, I still get the same errors. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload