阅读从MATLAB文件中的数据转换成C [英] Reading data from matlab files into C

查看:200
本文介绍了阅读从MATLAB文件中的数据转换成C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何使用C API来读取Matlab的 .MAT 文件,但它不工作如我所料:

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

我想只要打开 test.mat 称为一个很简单的 .MAT 文件,读取值从该文件并将其存储在C变量。我用下面的命令来创建 test.mat 在Matlab:

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")

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

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

编译器的输出:

$ 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标志与行指定 CPPFLAGS = -I在/ usr /本地/ MATLAB / R2011a /的extern /包括/ 在我的makefile,和我已经证实,该目录存在并且包含头文件 mat.h matrix.h )。

(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).

更新:结果
我发现,我需要连接在库 libmat.so libmx.so (按< A HREF =htt​​p://www.mathworks.se/help/techdoc/matlab_external/f19027.html>这个MathWorks的帮助文章),居住在的/ usr /本地/ MATLAB / R2011a /斌/ glnxa64 / 我的系统上。因此,我已经更新了我的makefile这样:

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?

推荐答案

这是一个连接失败,而不是一个编译失败(并且是无关的 -I 编译器选项) 。你需要指定其中MATLAB 的.so 文件使用 -L 标记所在的目录,并添加 -l&LT; MATLAB的LIB-名称&gt; 选项结束指定的matlab库的名称编译器命令

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.

例如:

CC -I在/ usr /本地/ MATLAB / R2011a /的extern /有/ -L的/ usr /本地/ MATLAB / R2011a / lib目录animate_shot.c -o animate_shot -lmatlab

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

(我不知道确切的目录复制到其中的.so位于或MATLAB库的名称)

根据注释提供进一步的信息:

Based on the comment providing further information:

CC -I在/ usr /本地/ MATLAB / R2011a /的extern /有/ -L的/ usr /本地/ MATLAB / R2011a /斌/ glnxa64 animate_shot.c -o animate_shot -lmat -lmx

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

这篇关于阅读从MATLAB文件中的数据转换成C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆