【问题标题】:Reading data from matlab files into C将matlab文件中的数据读入C
【发布时间】:2012-08-30 16:06:41
【问题描述】:

我正在尝试学习如何使用 C API 来读取 Matlab .mat 文件,但它没有按我的预期工作:

我只想打开一个名为test.mat 的非常简单的.mat 文件,从文件中读取一个值并将其存储在一个C 变量中。我使用以下命令在 Matlab 中创建了test.mat

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

下面是我的 C 代码,它甚至无法编译 - 编译器似乎找不到头文件。请参阅下面的编译器输出代码。我在这里做错了什么?

代码:

#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;
}

编译器输出:

$ 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

(-I 标志在我的 makefile 中使用 CPPFLAGS=-I/usr/local/MATLAB/R2011a/extern/include/ 行指定,并且我已验证该目录存在并包含头文件 mat.hmatrix.h)。

更新:
我发现我需要链接的库是libmat.solibmx.so(根据this MathWorks help article),位于我系统上的/usr/local/MATLAB/R2011a/bin/glnxa64/。因此,我将我的 makefile 更新为:

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

现在,运行 make 会给出以下命令:

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

但是,我仍然遇到同样的错误。有什么想法吗?

【问题讨论】:

    标签: c matlab


    【解决方案1】:

    这是链接器故障,而不是编译器故障(与-I 编译器选项无关)。您需要使用-L 标志指定matlab .so 文件所在的目录,并在指定matlab 库名称的编译器命令末尾添加-l&lt;matlab-lib-name&gt; 选项。

    例如:

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

    (我不知道 .so 所在的确切目录或 matlab 库的名称)


    根据提供更多信息的评论:

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

    【讨论】:

    • 好的。我找到了 *.so 所在的位置 (/usr/local/MATLAB/R2011a/bin/glnxa64/),我想我是 figured out what the libraries are called (libmat.solibmx.so)。但是,我仍然无法编译(得到与以前相同的错误)。我已经用我所做更改的一些详细信息更新了我的答案。
    • @TomasLycken,您的问题没有更新。根据你所说的,我已经更新了我的答案。
    • 对不起 - 我写了评论,开始写更新,忘记点击“保存”:P 现在它在那里。它似乎正在按照您的建议进行操作,但仍然无法正常工作......
    • @TomasLycken,-lmat 之间有空格字符吗?如果是这样,请将其删除:-lmat -lmx。不确定这是否重要,但 Sun Forte 编译器的帮助(我认为您正在使用)没有显示空格。
    • @TomasLycken,-l 选项需要位于编译器命令的 end (请参阅此答案了解原因:stackoverflow.com/questions/9966959/…)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多